如何找出渐变中的所有颜色?

发布于 2024-12-24 03:34:02 字数 330 浏览 3 评论 0原文

可能的重复:
Javascript颜色渐变

我有颜色一(比如说黄色)和颜色二(蓝色) - 它们组成渐变。
基于 0 到 100 的值(0 表示黄色,100 表示蓝色),我想表示颜色一和颜色二的混合。

我正在尝试在移动浏览器(特别是 safari)中执行此操作。

有没有办法在 JavaScript 中做到这一点?

Possible Duplicate:
Javascript color gradient

I have color one (let's say yellow) and color two (blue) - they make up a gradient.
Based on a value of 0 to 100, (0 being yellow and 100 being blue), I'd like to represent a mixture of color one and two.

I am trying to do this in a mobile browser (safari specifically).

Is there a way to do this in javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2024-12-31 03:34:02

如果您想要创建一种颜色,该颜色在两种其他颜色之间具有一定的百分比 (0-100),您可以使用以下 javascript 来实现:

function makeGradientColor(color1, color2, percent) {
    var newColor = {};

    function makeChannel(a, b) {
        return(a + Math.round((b-a)*(percent/100)));
    }

    function makeColorPiece(num) {
        num = Math.min(num, 255);   // not more than 255
        num = Math.max(num, 0);     // not less than 0
        var str = num.toString(16);
        if (str.length < 2) {
            str = "0" + str;
        }
        return(str);
    }

    newColor.r = makeChannel(color1.r, color2.r);
    newColor.g = makeChannel(color1.g, color2.g);
    newColor.b = makeChannel(color1.b, color2.b);
    newColor.cssColor = "#" + 
                        makeColorPiece(newColor.r) + 
                        makeColorPiece(newColor.g) + 
                        makeColorPiece(newColor.b);
    return(newColor);
}

此函数假设渐变是通过每个 r、g 之间的线性插值来实现的和两个端点颜色的 b 通道值,使得 50% 梯度值是每个 r、g、b 值的中点(所呈现的两种颜色之间的中间)。一次也可以制作不同类型的渐变(使用不同的插值函数)。

要将此结果分配给背景,您可以使用我添加到返回结果中的 CSS 颜色值,如下所示:

// sample usage
var yellow = {r:255, g:255, b:0};
var blue = {r:0, g:0, b:255};
var newColor = makeGradientColor(yellow, blue, 79);
element.style.backgroundColor = newColor.cssColor;

If what you're trying to do is to create a color that is some percentage (0-100) between two other colors, you can do that with this javascript:

function makeGradientColor(color1, color2, percent) {
    var newColor = {};

    function makeChannel(a, b) {
        return(a + Math.round((b-a)*(percent/100)));
    }

    function makeColorPiece(num) {
        num = Math.min(num, 255);   // not more than 255
        num = Math.max(num, 0);     // not less than 0
        var str = num.toString(16);
        if (str.length < 2) {
            str = "0" + str;
        }
        return(str);
    }

    newColor.r = makeChannel(color1.r, color2.r);
    newColor.g = makeChannel(color1.g, color2.g);
    newColor.b = makeChannel(color1.b, color2.b);
    newColor.cssColor = "#" + 
                        makeColorPiece(newColor.r) + 
                        makeColorPiece(newColor.g) + 
                        makeColorPiece(newColor.b);
    return(newColor);
}

This function assumes the gradient is made with linear interpolation between each r, g and b channel value of the two endpoint colors such that the 50% gradient value is the midpoint of each r,g,b value (halfway between the two colors presented). Once could make different types of gradients too (with different interpolation functions).

To assign this result to a background, you use the CSS color value I've added to the return result like this:

// sample usage
var yellow = {r:255, g:255, b:0};
var blue = {r:0, g:0, b:255};
var newColor = makeGradientColor(yellow, blue, 79);
element.style.backgroundColor = newColor.cssColor;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文