Solved

PHP:  change color string

Posted on 2010-11-25
7
312 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

810 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