SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    May 2003
    Location
    Here
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Interpreting color tags

    I want to be able to freely change the background color of a page and have PHP set the font color so that it is readable.

    The way I think this would work would be to read the color tag (e.g., #FFCC00), parse it (e.g., FF, CC and 00) and convert it to decimal numbers (e.g., 255, 204, 0). I could then average the numbers, and if greater than 128, insert a dark font, or if less than 128, insert a light font. Does that make sense? If I want to get tricky, I could set up an algorithm to choose a complimentary color, but I doubt that would be worthwhile.

    Can anyone help me with the two basic problems: Parse the text string into hexadecimal segments, and turn those segments into decimal numbers I can fiddle with. The rest I think I can handle. (Or if you can come up with a better plan altogether, that would be fine. )

    Thanks in advance!

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str '#FAFEBF';

    $str str_replace('#'''$str);
    for (
    $i 0$i 3$i++) {
        
    $color[$i] = hexdec(substr($str02));
        
    $str substr_replace($str''02);
    }

    list(
    $red$green$blue) = $color;
    print(
    $red);
    print(
    $green);
    print(
    $blue); 
    Hope that helps!
    - website

  3. #3
    SitePoint Enthusiast Powerlord's Avatar
    Join Date
    May 2003
    Location
    Mason, MI, USA
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by myNameIsDoug
    I want to be able to freely change the background color of a page and have PHP set the font color so that it is readable.

    The way I think this would work would be to read the color tag (e.g., #FFCC00), parse it (e.g., FF, CC and 00) and convert it to decimal numbers (e.g., 255, 204, 0). I could then average the numbers, and if greater than 128, insert a dark font, or if less than 128, insert a light font. Does that make sense? If I want to get tricky, I could set up an algorithm to choose a complimentary color, but I doubt that would be worthwhile.

    Can anyone help me with the two basic problems: Parse the text string into hexadecimal segments, and turn those segments into decimal numbers I can fiddle with. The rest I think I can handle. (Or if you can come up with a better plan altogether, that would be fine. )

    Thanks in advance!
    Are you changing the color on an external page, or one under your own control? I ask because CSS (Cascading Style Sheets) supports using decimal numbers to set colors using rgb(254, 204, 0) syntax. If these pages are under your control, you might be able to use this as a solution.

  4. #4
    SitePoint Member
    Join Date
    May 2003
    Location
    Here
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    website: Thanks, that works perfectly.

    Powerlord: That's a good idea, but I already have my colors set up with traditional tags.

    My thanks to you both.


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
  •