Solved

PHP:  change color string

Posted on 2010-11-25
7
310 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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 500 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now