繁体   English   中英

加权平均混色

[英]Weighted average colour mixing

(对不起,不确定标题的措辞)。

我要制作一个进度条类型的东西,它在屏幕上移动时会从绿色-橙色-红色变化。 例如:

在此处输入图片说明

如何找到给定点的颜色?

提前致谢!

编辑 :这是实现此功能的jsfiddle的链接: http : //jsfiddle.net/EAM9a/

对于最简单的方法,您可以在颜色之间进行线性插值。 让我们假设进度从0.0到1.0变得容易。 所以

0.0 - green  - rgb(0,   100, 0)
0.5 - orange - rgb(255, 165, 0)
1.0 - red    - rgb(139,   0, 0)

然后,我们可以根据需要的颜色在绿色和橙色之间或在橙色和红色之间进行插值。

var  green = [0, 100, 0],
    orange = [255, 165, 0],
       red = [139, 0, 0];

function color(val) {
  if (val < 0.5) {
    return colorToString(interpolate(val * 2, green, orange));
  } else {
    return colorToString(interpolate((val-0.5) * 2, orange, red));
  }
}

// val should be in the range [0.0, 1.0]
// rgb1 and rgb2 should be an array of 3 values each in the range [0, 255]
function interpolate(val, rgb1, rgb2) {
  var rgb = [0,0,0];
  var i;
  for (i = 0; i < 3; i++) {
    rgb[i] = rgb1[i] * (1.0 - val) + rgb2[i] * val;
  }
  return rgb;
}

// quick helper function to convert the array into something we can use for css
function colorToString(rgb) {
  return "rgb(" + rgb[0] + ", " + rgb[1] + ", " + rgb[2] + ")";
}

我为您创建了自己的颜色查找器:

它接受开始和结束色相,位置数和当前位置,并返回CSS颜色值。

看到它在摆弄

function restrict(value, low, high) {
    return value < low ? low : (value > high ? high : value);
}
function interpolate(rangeLow, rangeHigh, inputLow, inputHigh, value) {
    return (value - inputLow) / (inputHigh - inputLow) * (rangeHigh - rangeLow) + rangeLow;
}
function pad2(value) {
    return ('0' + value).substr(-2, 2);
}
function webColorFromRGB(color) {
    return '#' + pad2(color.R.toString(16)) + pad2(color.G.toString(16)) + pad2(color.B.toString(16));
}
function floatModulo(value, modulo) {
    return value - Math.floor(value / modulo) * modulo;
}
function webColorFromPosition(fromHue, toHue, positions, position) {
    var hue = interpolate(fromHue, toHue, 0, positions, position),
        RGB = {R: 0, G: 240, B: 120};
    for (var c in RGB) {
        if (!RGB.hasOwnProperty(c)) { continue; }
        RGB[c] = Math.round(interpolate(0, 255, 0, 360, restrict(Math.abs(floatModulo(hue + RGB[c], 360) * 6 - 1080) - 360, 0, 360)), 0);
    }
    return webColorFromRGB(RGB);
}

特征:

  • 接受从0到360的开始和结束色调。
  • 结束可以低于开始。
  • 如果要多次旋转色相,可以使用> 360的值

看起来是这样的:

Erik的自定义色彩查找器

希望我的功能对您有用!

我对@BenTaitelbaum方法进行了一些更改:

var fill = $("#progressFill");

var rgb = [],
    red = [255, 0, 0],
    green = [0, 255, 0],
    yellow = [255, 255, 0];

function color(val) {
    if (val < 0.5) {
        return colorToString(interpolate(val * 2, green, yellow));
    } else {
        return colorToString(interpolate((val - 0.5) * 2, yellow, red));
    }
}

function interpolate(val, rgb1, rgb2) {
    for (var i = 0; i < 3; i++) {
        rgb[i] = Math.floor(rgb1[i] * (1.0 - val) + rgb2[i] * val);
    }
    return rgb;
}

// quick helper function to convert the array into something we can use for css
function colorToString(rgb) {
    return "rgb(" + rgb[0] + ", " + rgb[1] + ", " + rgb[2] + ")";
}

$(function () {
    $({progress: 0}).animate({progress: 1}, {
        duration: 4000,
        step: function (now, fx) {
            fill.css({
                width: (now * 100) + "%",
                backgroundColor: color(now)
            });
        }
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM