Jump to content

Weird color question for you...


charris1980

Recommended Posts

right now i have a user choose a background color and a lighter background color to show next to it on a data row i am displaying.

 

i was curious if there was a way that they could choose a background color (lets say blueish) and then through php code go about 4-6 shades lighter of the blueish color?

 

Or if there is a different way to accomplish this I am open ears.  I just want to eliminate users input as much as possible.

Link to comment
Share on other sites

Yes, it will invert the numbers.

 

function negativeColor($color)
{
    //get red, green and blue
    $r = substr($color, 0, 2);
    $g = substr($color, 2, 2);
    $b = substr($color, 4, 2);
   
    //revert them, they are decimal now
    $r = 0xff-hexdec($r);
    $g = 0xff-hexdec($g);
    $b = 0xff-hexdec($b);
   
    //now convert them to hex and return.
    return dechex($r).dechex($g).dechex($b);
}

 

 

Link to comment
Share on other sites

Actually i did a test, and it did pretty well

<?php

function NegateColor($color)
{
return substr('000000'.dechex(~hexdec($color)),-6);
}

  $colors=array('FFFFFF','000000','3300FF','660033','444444','336633');
  echo "<TABLE>\n";
  foreach($colors as $color)
  {
  $bgc = NegateColor($color);
  echo "<TR><TD BGCOLOR=\"#$bgc\"><FONT color=\"#$color\">$color on $bgc</FONT></TD></TR>\n";
  }
  echo "</TABLE>";
  
?>

Link to comment
Share on other sites

Went a little further to generate a random base color, and it works most of the time, some colors just blend into each other too well.

 

<?php

function NegateColor($color)
{
return substr('000000'.dechex(~hexdec($color)),-6);
}

  echo "<TABLE>\n";
  for($i=0;$i<10;$i++)
  {
  $color=dechex(mt_rand(0,hexdec('FFFFFF')));
  $bgc = NegateColor($color);
  echo "<TR><TD BGCOLOR=\"#$bgc\"><FONT color=\"#$color\">$color on $bgc</FONT></TD></TR>\n";
  }
  echo "</TABLE>";
  
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.