Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,963
    Thanks
    120
    Thanked 76 Times in 76 Posts

    IE 8, is null or not an object

    IN FF and GC all OK. What is the point ?

    '2' is null or not an object (on line 6 of code)
    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);
    }
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Try this:-

    Code:
    <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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,963
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Nope.
    Code:
    yours:         255  204  0  #3541120 0 
    MyOldrgbtohex :255  204  0  #ffcc00
    Code:
    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:

    Code:
     b = arr[2].replace(/ /g,' ').replace(/\)/g,' ')
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.
    Last edited by Philip M; 11-29-2012 at 01:41 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    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
    Code:
    <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
    Last edited by 007julien; 11-29-2012 at 05:23 PM. Reason: complement

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Ah, now I understand the purpose!

    Code:
    <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>
    Last edited by Philip M; 11-29-2012 at 05:49 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •