...

View Full Version : IE 8, is null or not an object



BubikolRamios
11-29-2012, 10:11 AM
IN FF and GC all OK. What is the point ?



'2' is null or not an object (on line 6 of code)




function RGBtoHEX1(str)
{
var arr = str.split(",");
r = arr[0].substring(4);
g = arr[1];
b = arr[2].replace(/ /g,' ').replace(/\)/g,' ')
return '#' + rgbToHex(r, g, b);
}

Philip M
11-29-2012, 11:50 AM
Try this:-


<script type = "text/javascript">

function rgb2hex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}

alert (rgb2hex(100,100,100)) ;

</script>

Quizmaster: The Garden of Gethsemane in Jerusalem is at the foot of the Mount of ... what?
Contestant: Everest.

BubikolRamios
11-29-2012, 01:02 PM
Nope.


yours: 255 204 0 #3541120 0
MyOldrgbtohex :255 204 0 #ffcc00




function rgbToHexOld(r, g, b) {
if (r > 255 || g > 255 || b > 255)
throw "Invalid color component";
return ((r << 16) | (g << 8) | b).toString(16);
}


besides it has nothing to do with problem, IE complains on this line:



b = arr[2].replace(/ /g,' ').replace(/\)/g,' ')

Philip M
11-29-2012, 01:36 PM
My code gives

alert (rgb2hex(255,204,0)) ; //#FFCC00

Did you copy it correctly?

r = arr[0].substring(4); // makes no sense when the array element length is 3.

I do not undersatnd what your code function RGBtoHEX1(str) is supposed to do.

007julien
11-29-2012, 05:17 PM
If I clear understand the question is not to transform the three numbers 255,204,0 to the string '#FFCC00' but to transform the string '255,204,0' or 'rgb(255,204,0)' to '#FFCC00'

Then I propose

<script type="text/javascript">
function RGBtoHEX1(str){var r,g,b;
str.replace(/([\d]{1,3}),([\d]{1,3}),([\d]{1,3})/,function(){r=arguments[1],g=arguments[2],b=arguments[3];)});
return rgb2hex(r, g, b);
}
function rgb2hex(r, g, b) {
return '#'+((r << 16) | (g << 8) | b).toString(16);
}
alert(RGBtoHEX1('rgb(250,204,0)'))
</script>The return could be in the replace callback function to avoid errors and return null

Philip M
11-29-2012, 05:44 PM
Ah, now I understand the purpose!


<script type = "text/javascript">

function RGBtoHEX1(str) {
str=str.replace(/[^\d\,]/gi, "");
var s = str.split(",");
var r = s[0]*1; // r, g and b must be numbers
var g = s[1]*1;
var b = s[2]*1;
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
}

alert (RGBtoHEX1('rgb(250,204,0)'))

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum