Results 1 to 7 of 7

Thread: JasaScript, CSS and reading color properties

  1. #1
    Join Date
    Dec 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JasaScript, CSS and reading color properties

    Subj.

    I've written something, but it's hackish.

    Code:
    function rgb(r, g, b)
    {
      return (r<<16) + (g<<8) + b;
    }
    
    function colorToInt(s)
    {
      if(s.charAt(0)=='#' && s.length==7)      // Opera
        return parseInt("0x"+s.substring(1));
    
      if(s.charAt(0)=='#' && s.length==4)      // IE
        return parseInt("0x"+s.charAt(1)+s.charAt(1)+s.charAt(2)+s.charAt(2)+s.charAt(3)+s.charAt(3));
    
      if(s.substring(0, 3)=="rgb")             // Netscape / Mozilla
        return eval(s);
    
      alert(s);                                // WTF
    }
    
    [...] var myColor = colorToInt(document.getElementById("myelement").style.color); [...]
    Is there a proper way to do this?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's not bad. What is your object in this? If the color is set in the stylesheet, through inheritance or via attribute, this:

    Code:
    var myColor = colorToInt(document.getElementById("myelement").style.color);
    will not pick it up. Also, what will you do with this integer once you have it?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well that's not much of an issue, that particular line I wrote out of my head to show the use of colorToInt.

    My goal was to fade a page's background and contents to white, but keeping it proportional.

    It's supposed to be for the loading intro of my homepage. You can find a working example of it here: http://maplecenter.net/thecybershadow/ (temporary URL).

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Just a tip for simplifying your full code on this, which looks quite impressive and will provide hours of enjoyment trying to fully follow - To get the hex color value of the output of colorToInt(s) or any color represented as such an integer as this and other functions in your code produce:

    Code:
    hexColorValue='#'+colorIntegerValue.toString(16)
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Wouldn't it be easier to return an array of three values?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Dec 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1
    Code:
    hexColorValue='#'+colorIntegerValue.toString(16)
    That isn't quite correct. Suppose the value is less than 100000? Then the number would have 5 or less digits. Which is NOT what we want.

    If you'll read fader.js on my site further, you'll find this function:

    Code:
    function toColor(i)
    {
      var s = i.toString(16)
      while(s.length<6)
        s='0'+s;
      return '#'+s;
    }

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by The_CyberShadow
    If you'll read fader.js on my site further, you'll find this function: . . .
    Quote Originally Posted by jscheuer1
    . . . which looks quite impressive and will provide hours of enjoyment trying to fully follow . . .


    'nuff said?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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