Solved

PHP:  change color string

Posted on 2010-11-25
7
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

751 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