Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP:  change color string

Posted on 2010-11-25
7
Medium Priority
?
318 Views
Last Modified: 2012-05-10
Hi All,

I have a string called $color, is there an way to find the next color up.

e.g.

$color = '#FFFFFF';

would give us;
$new_color = '#EEEEEE';



I was thinking of maybe looping the string like below
<?php
$color = 'FFFFFF';

$new_color = substr($color,0,1);

switch ($new_color) {
    case "F":
        echo $new_color = 'E';
        break;
    case "E":
        echo $new_color = 'D';
        break;
    case "D":
        echo $new_color = 'C';
        break;
    case "C":
        echo $new_color = 'B';
        break;
    case "B":
        echo $new_color = 'A';
        break;
    case "A":
        echo $new_color = '9';
        break;
    case "9":
        echo $new_color = '8';
        break;
    case "8":
        echo $new_color = '7';
        break;
    case "7":
        echo $new_color = '6';
        break;
    case "6":
        echo $new_color = '5';
        break;
    case "5":
        echo $new_color = '4';
        break;
    case "4":
        echo $new_color = '3';
        break;
    case "3":
        echo $new_color = '2';
        break;
    case "2":
        echo $new_color = '1';
        break;
    case "1":
        echo $new_color = '0';
        break;
    case "0":
        echo $new_color = '1';
        break;
}

echo $new_color;
?>

Open in new window

0
Comment
Question by:detox1978
  • 5
  • 2
7 Comments
 
LVL 8

Expert Comment

by:ropenner
ID: 34214764
//to show all colors (or take steps of 50) you can do this in PHP

<?PHP
$step = 50;
for ($i=0; $i<255; $i+=$step) {
      $red_portion = base_convert($i, 16, 10); // convert number $i from base 16 (hexidecimal) to base 10 (decimal)
      for ($j=0; $j<255; $j+=$step) {      
            $green_portion = base_convert($j, 16, 10);
            for ($k=0; $k<255; $k+=$step) {      
                  $blue_portion = base_convert($k, 16, 10);
                  print "<FONT color=#$red_portion$green_portion$blue_portion><B>O</B></FONT> ";
            }
      }
}
?>



colour is  Red Green and Blue stuck together in one string.

So in the color #FFEEDD

FF = red
EE = green
DD = blue

These are hexidecimal numbers indicating the intensity of each color which is why FFFFFF is white because all three colours are at maximum (255 decimal == FF hex)

What do you mean by 'next color up'... in your example you go from FFFFFF which is white to EEEEEE which is slightly offwhite (slightly gray).

FF0000 is red only
00FF00 is green only
0000FF is blue only

if you cycle through the hexidecimal numbers 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F you get all colours as in my php code example above.
0
 
LVL 8

Expert Comment

by:ropenner
ID: 34214775
sorry, did the base_conversion backwards

<?PHP
$step = 50;
for ($i=0; $i<255; $i+=$step) {
      $red_portion = base_convert($i, 10, 16); // convert number $i from base 10 (decimal) to base 16 (hexidecimal)
      for ($j=0; $j<255; $j+=$step) {      
            $green_portion = base_convert($j, 10, 16);
            for ($k=0; $k<255; $k+=$step) {      
                  $blue_portion = base_convert($k, 10, 16);
                  print "<FONT color=#$red_portion$green_portion$blue_portion><B>O</B></FONT> ";
            }
      }
}
?>
0
 
LVL 2

Author Comment

by:detox1978
ID: 34214783
Hi,

When i say next level up.  I'm looking for the nearest color.

So

F = E
E= D
D = C
C = B
B = A
A = 9
9 = 8
8 = 7
7 = 6
6 = 5
5 = 4
4 = 3
3 = 2
2 = 1
0 = 1


So #FE34DD would be  #ED23CC
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ropenner
ID: 34214784
third times the charm ... I didn't pad the number with zeros like should have been done the first time
<?PHP
$step = 50;
for ($i=0; $i<255; $i+=$step) {
      $red_portion = base_convert($i, 10, 16); // convert number $i from base 16 (hexidecimal) to base 10 (decimal)
      for ($j=0; $j<255; $j+=$step) {      
            $green_portion = base_convert($j, 10, 16);
            for ($k=0; $k<255; $k+=$step) {      
                  $blue_portion = base_convert($k, 10, 16);
                  print "<FONT color=#".str_pad($red_portion,2,'0').str_pad($green_portion,2,'0').str_pad($blue_portion,2,'0')."><B>O</B></FONT> ";
            }
      }
}
?>
0
 
LVL 8

Accepted Solution

by:
ropenner earned 2000 total points
ID: 34214811
your sample is getting a slightly darker colour with similar amounts of blue red and green.

essentially subtracting 1 (hex) from each digit ... except for 1's


<?PHP
$color = "#FD0942";
$hexnumbers = array(0,0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E');
print "$color<BR>\n";
$new_color = "#";
for ($i=1; $i< strlen($color);$i++) {
      $hexdigit = $color[$i];
      $new_hex = $hexnumbers[hexdec($hexdigit)];
      $newnumber .= "$new_hex";
}
print "$newnumber\n<BR>";
?>

0
 
LVL 2

Author Comment

by:detox1978
ID: 34214864
Thanks,

Added a line to make 0's into 2's, so they drop into 1's
<?PHP
$color = "#000222";
$color = str_replace('0','2',$color); // hack 0 to 2, so it drops to 1
$hexnumbers = array(0,0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E');
print "$color<BR>\n";
$new_color = "#";
for ($i=1; $i< strlen($color);$i++) {
      $hexdigit = $color[$i];
      $new_hex = $hexnumbers[hexdec($hexdigit)];
      $newnumber .= "$new_hex";
}
print "$newnumber\n<BR>";
?>

Open in new window

0
 
LVL 8

Expert Comment

by:ropenner
ID: 34214910
save a line of code if you just change this

$hexnumbers = array(0,0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E');

to

$hexnumbers = array(1,1,1,2,3,4,5,6,7,8,9,'A','B','C','D','E');

because what it says is that index 0 becomes a 1 and index 1 becomes a 1 and index 2 becomes a one and index 3 becomes a 2.... etc.   Then you won't get 0's either.

I also had an error where
$new_color = "#"; should have been
$newnumber="#";

the whole loop innerds can be one line as well but it is more readable in its current form

example of for loop insides as one line:
$newnumber .= $hexnumbers[hexdec($color[$i])];
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question