4

I want an array where each field in the array contains a color code

array(0 => '#4CFF00', 1 => '#FFE97F')

And I want this to go through the whole spectrum of colors starting from green to black.

green-> blue -> dark blue -> purple -> yellow -> orange -> red -> brown ->black

This order doesn't need to be exactly the same, but I think you get the picture. Can anybody help with this? Is there a website that has done this before?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Kennethvr
  • 2,660
  • 5
  • 26
  • 35
  • there's an awful lot of colours in the spectrum from from green to black. Do you want them all or just the ones you've listed? – Glen Sep 21 '09 at 15:27
  • 1
    You know those color charts where it looks like a rainbow and the colors smoothly transition from one shade to another? I think that is what he wants, but he wants to generate it in an array. – John B Sep 21 '09 at 15:34
  • from green to black? your `#FFE97F` is white … `#4CFF00` is green though. so what’s up with the black? wrong code example? or wrong understanding on my side? – knittl Sep 22 '09 at 07:42

9 Answers9

10

You should use a colour model like Hue-Saturation-Value (HSV), and cycle the hue from 0 degrees all the way around the spectrum to 360 degrees, at which whatever saturation and value suited you. (If you want to go from green->green, just start at 120 degrees)

Here's an illustration which shows the difference between RGB and HSV based gradients: the top gradient is just going from green to red in an RGB model, but the lower one uses HSV, resulting in a more pleasing effect.

alt text

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
1

00FF00 is Green 000000 is Black. all you have to do it increment one color at a time while decrementing the other colors. Stick it in a loop, where it be php, javascript or whatever and go.

EDIT: Here is a link to code that shows how to loop through Hex color codes.

WolfmanDragon
  • 7,851
  • 14
  • 49
  • 61
1
function dechexpad($i){
$s="";
if($i<16) $s="0";
$s.=dechex($i);
return $s;
}
function hexcolor($r,$g,$b){
return "#".dechexpad($r%255).dechexpad($g%255).dechexpad($b%255);
}

$xx=array();
for($i=0;$i<255;$i++){
$xx[]=hexcolor($i*4,$i*2,$i);
}
n00b
  • 5,642
  • 2
  • 30
  • 48
1
function list_colours($start, $end, $steps = 6)
{
    $return = array();

    $start_r = hexdec(substr($start, 1, 2));
    $start_g = hexdec(substr($start, 3, 2));
    $start_b = hexdec(substr($start, 5, 2));

    $end_r = hexdec(substr($end, 1, 2));
    $end_g = hexdec(substr($end, 3, 2));
    $end_b = hexdec(substr($end, 5, 2));

    $shift_r = ($end_r - $start_r) / $steps;
    $shift_g = ($end_g - $start_g) / $steps;
    $shift_b = ($end_b - $start_b) / $steps;

    for ($i = 0; $i < $steps; $i++)
    {
        $color = array();
        $color[] = dechex($start_r + ($i * $shift_r));
        $color[] = dechex($start_g + ($i * $shift_g));
        $color[] = dechex($start_b + ($i * $shift_b));

        // Pad with zeros.
        $color = array_map(function ($item) {
                return str_pad($item, 2, "0", STR_PAD_LEFT);
            },
            $color
        );

        $return[] = '#' . implode($color);
    }

    return $return;
}

// Examples
$spectrum = array();
$spectrum[] = list_colours("#000000", "#FFFFFF"); // grey
$spectrum[] = list_colours("#cc0033", "#FFFFFF"); // R
$spectrum[] = list_colours("#ff6600", "#FFFFFF"); // O
$spectrum[] = list_colours("#fdc710", "#FFFFFF"); // Y
$spectrum[] = list_colours("#cccc00", "#FFFFFF"); // G
$spectrum[] = list_colours("#339933", "#FFFFFF"); // G dark
$spectrum[] = list_colours("#339999", "#FFFFFF"); // B teal
$spectrum[] = list_colours("#14acde", "#FFFFFF"); // B light
$spectrum[] = list_colours("#0066cc", "#FFFFFF"); // B dark
$spectrum[] = list_colours("#663399", "#FFFFFF"); // I dark
$spectrum[] = list_colours("#990066", "#FFFFFF"); // I light
$spectrum[] = list_colours("#cc0066", "#FFFFFF"); // V pink
Corey
  • 1,977
  • 4
  • 28
  • 42
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
0

You need some color schemes? I think it depends on the color scheme of the rest of your page. You can google "Color scheme generators" and you can find at least 10. Examples provided by @edg and @santiiiii above.

If you have a photo, there is a tool which can get colors from the photo to fit your site.

http://www.atalasoft.com/31apps/ColorSchemeGenerator/
Lukasz Lysik
  • 10,462
  • 3
  • 51
  • 72
0

I recommend writing a function to do this. If you need to go between multiple colors, just call it multiple times and concatenate the arrays.

Psuedocode: `colorRange(arrayReference, startColor, endColor, numSteps)`

Get {R, G, B} startColor and endColor
Get R_diff, G_diff, and B_diff

for i in 0 to numSteps {
   arrayReference.append(
          i => {min(startR, endR) + (R_diff * (i / numSteps)),
                min(startG, endG) + (G_diff * (i / numSteps)),
                min(startB, endB) + (B_diff * (i / numSteps))}
          )
}
Steven
  • 13,501
  • 27
  • 102
  • 146
0

Look at hexdec, dechex and this snippet:

for ($i = 0; $i < 255; $i++) {
    # here happens some colour-magic
}

Cheers,

Boldewyn
  • 81,211
  • 44
  • 156
  • 212
  • 1
    if `// here happens some *** magic` was the answer to everything, the world would be a better place -.- – knittl Sep 22 '09 at 07:44
  • Yep, because that would mean, that people wanting to accomplish some task would google "hex color code" and try and learn themselves instead of just asking as the first option. – Boldewyn Sep 22 '09 at 08:17
0

I guess if you need all of those colors you could build the 16776960 element array as follows:

<?php
    $end = 'FFFFFF';

    $arrMassiveColor = array();

    for($curr = 0; $curr <= dechex($end); $curr++) {
        $arrMassiveColor[] = '#'.hexdec($curr);
    }
?>

It seems a bit odd though...

Buggabill
  • 13,726
  • 4
  • 42
  • 47
0

I stumbled upon this question - and surprisingly I could not find an actual solution for this on the web (I did not try hard). Performance is besides the point here - this is not what you would want to do - this is just an exercise - but if you actually wanted to iterate over all hex colors - here is how you could do it:

for($i = 0; $i <= 16777215; $i++) {
    echo sprintf('%06s', dechex($i));
}

Of course this question is old and answered, but thought I would share this anyway.