HTML颜色代码:红色到黄色到绿色

Jus*_*ude 38 html hex colors scale

我想提出尽可能多的HEX HTML值,以获得从红色到绿色的平滑颜色渐变:

我希望这类似于以下内容:http: //www.utexas.edu/learn/html/colors.html

我没有最好的颜色选择,所以我希望标准图表已经放在一起显示如何顺利地从红色到黄色过渡到绿色.

在该网站上,"1 of 6"与我正在寻找的最相似,但该示例仅限于11种颜色:

(1) FF0000 Red, 
(2) FF3300 Red(Orange)
(3) ff6600 
(4) ff9900 
(5) FFCC00 Gold 
(6) FFFF00 Yellow
(7) ccff00
(8) 99ff00
(9) 66ff00
(10) 33ff00
(11) 00FF00 Lime 
Run Code Online (Sandbox Code Playgroud)

能够将颜色数量增加一倍,但能够顺利过渡,真是太棒了.

感谢您的任何见解和帮助.

jba*_*all 46

根据您想要结束的颜色数量,解决方案只是将绿色值增加一定量,然后当绿色是maxed(FF)时,重复减少红色值相同的量.

伪代码:

int red = 255; //i.e. FF
int green = 0;
int stepSize = ?//how many colors do you want?
while(green < 255)
{
    green += stepSize;
    if(green > 255) { green = 255; }
    output(red, green, 0); //assume output is function that takes RGB
}
while(red > 0)
{
    red -= stepSize;
    if(red < 0) { red = 0; }
    output(red, green, 0); //assume output is function that takes RGB
}
Run Code Online (Sandbox Code Playgroud)

手动生成,您可以简单地增加16,如下所示:

FF0000
FF1000
FF2000
FF3000
FF4000
FF5000
FF6000
FF7000
FF8000
FF9000
FFA000
FFB000
FFC000
FFD000
FFE000
FFF000
FFFF00 //max, step by 15
F0FF00 //cheat, start with a -15 to simplify the rest
E0FF00
D0FF00
C0FF00
B0FF00
A0FF00
90FF00
80FF00
70FF00
60FF00
50FF00
40FF00
30FF00
20FF00
10FF00
Run Code Online (Sandbox Code Playgroud)


Asa*_*aph 26

最好的方法是了解十六进制颜色代码的实际含义.一旦掌握了这一点,就会清楚如何制作任意平滑度的渐变.十六进制颜色代码是三元组,分别代表颜色的红色,绿色和蓝色分量.因此,例如在颜色中FF0000,红色成分是FF绿色成分00,蓝色成分是00.FF0000看起来很红,因为红色组件一直拨到最后,FF绿色和蓝色一直拨到00.同样,纯绿色00FF00和纯蓝色0000FF.如果将十六进制数转换为十进制数,则会在0和之间得到一个值255.

那么现在如何使梯度从红色变为黄色变为绿色?简单; 你取出终点,决定你想要的步数,然后均匀地逐步通过3个颜色通道中的每一个,从一种颜色过渡到下一种颜色.下面是以11十六进制(17十进制)为步骤的示例:

FF0000 <-- red
FF1100
FF2200
FF3300
FF4400
FF5500
FF6600
FF7700
FF8800
FF9900
FFAA00
FFBB00
FFCC00
FFDD00
FFEE00
FFFF00 <-- yellow
EEFF00
DDFF00
CCFF00
BBFF00
AAFF00
99FF00
88FF00
77FF00
66FF00
55FF00
44FF00
33FF00
22FF00
11FF00
00FF00 <-- green
Run Code Online (Sandbox Code Playgroud)


Asc*_*ant 10

我刚做了一个项目,开始时或多或少类似于jball和Asaph的解决方案.也就是说,从红色(FF0000)平滑地增加到(FFFF00)到(00FF00).

然而,我发现,在视觉上,"黄色"周围的变化似乎更加激烈,而在"红色"和"绿色"周围几乎看不到它们.我发现我可以通过使变化呈指数而不是线性来补偿这一点,导致增量在"黄色"周围变小,在"红色"和"绿色"周围变大.我制定的解决方案(在Javascript中)看起来像这样:

    /**
     * Converts integer to a hexidecimal code, prepad's single 
     * digit hex codes with 0 to always return a two digit code. 
     * 
     * @param {Integer} i Integer to convert 
     * @returns {String} The hexidecimal code
     */
    function intToHex(i) {
        var hex = parseInt(i).toString(16);
        return (hex.length < 2) ? "0" + hex : hex;
    }   

    /**
     * Return hex color from scalar *value*.
     *
     * @param {float} value Scalar value between 0 and 1
     * @return {String} color
     */
    function makeColor(value) {
        // value must be between [0, 510]
        value = Math.min(Math.max(0,value), 1) * 510;

        var redValue;
        var greenValue;
        if (value < 255) {
            redValue = 255;
            greenValue = Math.sqrt(value) * 16;
            greenValue = Math.round(greenValue);
        } else {
            greenValue = 255;
            value = value - 255;
            redValue = 256 - (value * value / 255)
            redValue = Math.round(redValue);
        }

        return "#" + intToHex(redValue) + intToHex(greenValue) + "00";
    }
Run Code Online (Sandbox Code Playgroud)

当我改变值时,这产生了更平滑的渐变,并且无论起点如何,将inputValue改变一定似乎都会将颜色或多或少地影响到相同的程度.

  • 很酷!但有一个奇怪之处:尝试将0.5传递给`makeColor`.你得到`#100ff00`!我采取的措施是将`redValue = 256 - (value*value/255)`改为`redValue = 255 - (value*value/255)`. (2认同)

Vla*_*sky 8

这是从绿色到红色的漂亮渐变

    /* Green - Yellow - Red */
    .gradient_0    {background: #57bb8a;}
    .gradient_5    {background: #63b682;}
    .gradient_10   {background: #73b87e;}
    .gradient_15   {background: #84bb7b;}
    .gradient_20   {background: #94bd77;}
    .gradient_25   {background: #a4c073;}
    .gradient_30   {background: #b0be6e;}
    .gradient_35   {background: #c4c56d;}
    .gradient_40   {background: #d4c86a;}
    .gradient_45   {background: #e2c965;}
    .gradient_50   {background: #f5ce62;}
    .gradient_55   {background: #f3c563;}
    .gradient_60   {background: #e9b861;}
    .gradient_65   {background: #e6ad61;}
    .gradient_70   {background: #ecac67;}
    .gradient_75   {background: #e9a268;}
    .gradient_80   {background: #e79a69;}
    .gradient_85   {background: #e5926b;}
    .gradient_90   {background: #e2886c;}
    .gradient_95   {background: #e0816d;}
    .gradient_100  {background: #dd776e;}

    /* Red - Yellow - Green */
    .anti-gradient_100  {background: #57bb8a;}
    .anti-gradient_95   {background: #63b682;}
    .anti-gradient_90   {background: #73b87e;}
    .anti-gradient_85   {background: #84bb7b;}
    .anti-gradient_80   {background: #94bd77;}
    .anti-gradient_75   {background: #a4c073;}
    .anti-gradient_70   {background: #b0be6e;}
    .anti-gradient_65   {background: #c4c56d;}
    .anti-gradient_60   {background: #d4c86a;}
    .anti-gradient_55   {background: #e2c965;}
    .anti-gradient_50   {background: #f5ce62;}
    .anti-gradient_45   {background: #f3c563;}
    .anti-gradient_40   {background: #e9b861;}
    .anti-gradient_35   {background: #e6ad61;}
    .anti-gradient_30   {background: #ecac67;}
    .anti-gradient_25   {background: #e9a268;}
    .anti-gradient_20   {background: #e79a69;}
    .anti-gradient_15   {background: #e5926b;}
    .anti-gradient_10   {background: #e2886c;}
    .anti-gradient_5    {background: #e0816d;}
    .anti-gradient_0    {background: #dd776e;}
Run Code Online (Sandbox Code Playgroud)
<div class="gradient_0">0</div>
<div class="gradient_5">5</div>
<div class="gradient_10">10</div>
<div class="gradient_15">15</div>
<div class="gradient_20">20</div>
<div class="gradient_25">25</div>
<div class="gradient_30">30</div>
<div class="gradient_35">35</div>
<div class="gradient_40">40</div>
<div class="gradient_45">45</div>
<div class="gradient_50">50</div>
<div class="gradient_55">55</div>
<div class="gradient_60">60</div>
<div class="gradient_65">65</div>
<div class="gradient_70">70</div>
<div class="gradient_75">75</div>
<div class="gradient_80">80</div>
<div class="gradient_85">85</div>
<div class="gradient_90">90</div>
<div class="gradient_95">95</div>
<div class="gradient_100">100</div>
Run Code Online (Sandbox Code Playgroud)