-1

well I am analyzing a javascript code, but I got confuse in some lines, for example I have this code

function getStyle(el,styleProp)
 {
var x = document.getElementById(el);
if (x.currentStyle)
    var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
    var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);

if (y.substr(0, 1) == "#"){ return y; } else {

var value = y.split(',');

var R = value[0].substr(4);
var G = value[1];
var B = value[2].substr(0, value[2].length-1);

var RGB =  "#" + toHex(R)+ toHex(G)+toHex(B);

return RGB;

 }
}


function toHex(N) {
if (N==null) return "00";
N=parseInt(N); if (N==0 || isNaN(N)) return "00";
N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
return "0123456789ABCDEF".charAt((N-N%16)/16)
  + "0123456789ABCDEF".charAt(N%16);
}




function pw (form)
{

var d1, d2, d3;

if (navigator.appName == "Netscape"){
d1= getStyle('content', 'background-color'); 
} else {
d1= getStyle('content', 'backgroundColor'); 
}
     d2=form.Name.value;
     d3=form.Name2.value;

Firs of all, I dont know what the variables "R", "G" and "B" are doing?, are they affecting the variable "d1"? I know that variables "d2" and "d3" are the values of what they said, but what is the value of the variable "d1"?

Some help will be appreciated. Tnx.

1
  • Don't try to analize it. Throw it over and rewrite it from scratch - neither the color extractor nor the hex converter are worth it. Commented Aug 6, 2012 at 2:17

2 Answers 2

1

This code is pretty straight forward. Variables R, G and B each contain a color, R - red, G - green and B - blue. This only happens if the style doesn't already represent the color through in a hex format. They are there to be able to convert color back to hex and return in. So yes, in a way they do affect the variable d1. If background-color style is going to be set like this: background-color: #FF00FF d1 is going to be #FF00FF. If the background color is going to be set like this: background-color: rgb(00, 255, 00) d1 is going to be #0000FF, since this function expects some none-existing format like this: background-color: 00, 255, 00 (this is not valid CSS).

In general this seems like a poorly named and written code.

Sign up to request clarification or add additional context in comments.

Comments

0

d1= getStyle('content', 'background-color'); will get background-color of content. If it in the form #000000 then just return it, if it in red green blue format (255,255,255) then R,G,B is used to store that value, then it convert to hex and return in hex form #000000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.