Solved

PHP:  change color string

Posted on 2010-11-25
7
315 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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
The viewer will learn how to count occurrences of each item in an array.

623 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