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
https://forums.phpfreaks.com/topic/95496-weird-color-question-for-you/
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);
}

 

 

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>";
  
?>

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>";
  
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.