How can I cycle through hex color codes in PHP?

前端 未结 9 2151
野的像风
野的像风 2021-01-06 06:08

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 t

相关标签:
9条回答
  • 2021-01-06 06:26

    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.

    0 讨论(0)
  • 2021-01-06 06:28

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

    0 讨论(0)
  • 2021-01-06 06:33

    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.

    0 讨论(0)
  • 2021-01-06 06:33

    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.

    0 讨论(0)
  • 2021-01-06 06:34

    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/
    
    0 讨论(0)
  • 2021-01-06 06:35

    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))}
              )
    }
    
    0 讨论(0)
提交回复
热议问题