算法:如何使用 RGB 值从红色通过黄色淡入绿色?

发布于 2024-11-16 07:15:02 字数 164 浏览 5 评论 0 原文

我想显示一种基于 0 到 100 之间的值的颜色。一端 (100) 是纯红色,另一端 (0) 是纯绿色。在中间(50),我希望它是黄色的。

我希望颜色逐渐从一种颜色褪到另一种颜色,例如在 75 时,颜色一半是红色,一半是黄色,等等。

如何对 RGB 值进行编程以反映这种褪色?谢谢。

I want to display a color based on a value from 0 to 100. At one end (100), it's pure Red, the other end (0), pure Green. In the middle (50), I want it to be yellow.

And I want the colors to fade gradually from one to another, such that at 75, the color is half red and half yellow, etc.

How do I program the RGB values to reflect this fading? Thanks.

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

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

发布评论

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

评论(18

小霸王臭丫头 2024-11-23 07:15:03

我将 1400 MHz 和 3500 MHz 之间的 CPU 速度值映射到 rgb() 值,以从 green ->黄色->红色与此功能

function green_yellow_red(core_MHz, core_id){
  var core_color = ~~core_MHz.map(1400, 3500, 0, 510)

  if(core_color < 255){
    $('#cpu_core_'+core_id).css('background', 'rgb('+core_color+',255 , 0)')
  }else{
    core_color-=255
    $('#cpu_core_'+core_id).css('background', 'rgb(255 ,'+ (255-core_color) +', 0)')
  }
}

I mapped values of CPU speed between 1400 MHz and 3500 MHz to rgb() values to get from green -> yellow -> red with this function

function green_yellow_red(core_MHz, core_id){
  var core_color = ~~core_MHz.map(1400, 3500, 0, 510)

  if(core_color < 255){
    $('#cpu_core_'+core_id).css('background', 'rgb('+core_color+',255 , 0)')
  }else{
    core_color-=255
    $('#cpu_core_'+core_id).css('background', 'rgb(255 ,'+ (255-core_color) +', 0)')
  }
}
陈甜 2024-11-23 07:15:03

更多的是相同的。只是 Delphi Pascal 编码并简化+锁定到信号量颜色(红/黄/绿)。

rectangle1.Fill.Color:=DefineColorSemaphore(newcolor);

function TForm1.DefineColorSemaphore(valperc:integer):TAlphaColor;
var
   vcol: TAlphaColorRec;
begin
  vcol.B := 0;    // blue:  always 0
  vcol.A := 255;  // alpha: 255=no
  if (valperc < 50) then
    begin
       // starts @ RGB=255,0,0 and increases G 0->255
       vcol.R := 255;
       vcol.G := trunc(255*valperc/50);
    end
    else
    begin
       // starts @ RGB=255,255,0 and decreases R 255->0
       vcol.R := 255-trunc(255* (valperc - 50)/50);
       vcol.G := 255;
    end;
  result:= TAlphaColor(vcol);
end;

More of the same. Just Delphi Pascal coded and simplified+locked to semaphore colors (red/yellow/green).

rectangle1.Fill.Color:=DefineColorSemaphore(newcolor);

function TForm1.DefineColorSemaphore(valperc:integer):TAlphaColor;
var
   vcol: TAlphaColorRec;
begin
  vcol.B := 0;    // blue:  always 0
  vcol.A := 255;  // alpha: 255=no
  if (valperc < 50) then
    begin
       // starts @ RGB=255,0,0 and increases G 0->255
       vcol.R := 255;
       vcol.G := trunc(255*valperc/50);
    end
    else
    begin
       // starts @ RGB=255,255,0 and decreases R 255->0
       vcol.R := 255-trunc(255* (valperc - 50)/50);
       vcol.G := 255;
    end;
  result:= TAlphaColor(vcol);
end;
酒儿 2024-11-23 07:15:03

最近我计划使用 Javascript 做同样的事情。我已经遵循了两个步骤。

  1. 首先在 0-1 范围内缩放值(scaledValue = (value - min) / (max - min))
  2. ,如果该值小于或等于 0.5,则增加红色通道的值,如果该值小于或等于 0.5,则减少绿色通道的值大于 0.5

这是我用于此目的的代码。

var blue = 0.0, red = 0.0, green = 0.0;

if(scaledValue <= 0.5)
{
    red = (scaledValue * 2) * 255.0;
    green = 255.0;
    blue = 0;
}else
{
    red = 255.0;
    green = 255.0 + 255.0 - ((scaledValue  * 2)* 255);
    blue = 0;
}

用于在 C++ 中显示此作品的示例代码

该解决方案的灵感来自 @jterrace 答案。

Recently I was planning to do samething using Javascript. I have followed two steps.

  1. First scale the value in 0-1 range(scaledValue = (value - min) / (max - min))
  2. Then increase value of Red channel if the value is less than or equal 0.5 and then decrease value of Green channel if the value is greater than 0.5

Here is the code I used for this purpose.

var blue = 0.0, red = 0.0, green = 0.0;

if(scaledValue <= 0.5)
{
    red = (scaledValue * 2) * 255.0;
    green = 255.0;
    blue = 0;
}else
{
    red = 255.0;
    green = 255.0 + 255.0 - ((scaledValue  * 2)* 255);
    blue = 0;
}

A sample code for showing this works in C++

This solution is inspired by @jterrace answer.

三岁铭 2024-11-23 07:15:03

上用红黄绿渐变填充 OwnerDraw 控件

int cx = lpDis->rcItem.right - lpDis->rcItem.left;

for (int x = 0; x < cx; x++)
{
    COLORREF cr;

    double d = 255 * 2 * (double)x/(double)cx;

    cr = x <= cx/2 ?    RGB(255,     d,   0) :
                        RGB(255 - d, 255, 0);

    HPEN hPen = CreatePen(PS_SOLID, 1, cr);
    HPEN hOldPen = (HPEN)SelectObject(lpDis->hDC, hPen);
    MoveToEx(lpDis->hDC, x, lpDis->rcItem.top, NULL);
    LineTo(lpDis->hDC, x, lpDis->rcItem.bottom);
    SelectObject(lpDis->hDC, hOldPen);
    DeleteObject(hPen);
}

// 在 WM_DRAWITEM Illustration

// To fill an OwnerDraw control with red-yellow-green gradient upon WM_DRAWITEM

int cx = lpDis->rcItem.right - lpDis->rcItem.left;

for (int x = 0; x < cx; x++)
{
    COLORREF cr;

    double d = 255 * 2 * (double)x/(double)cx;

    cr = x <= cx/2 ?    RGB(255,     d,   0) :
                        RGB(255 - d, 255, 0);

    HPEN hPen = CreatePen(PS_SOLID, 1, cr);
    HPEN hOldPen = (HPEN)SelectObject(lpDis->hDC, hPen);
    MoveToEx(lpDis->hDC, x, lpDis->rcItem.top, NULL);
    LineTo(lpDis->hDC, x, lpDis->rcItem.bottom);
    SelectObject(lpDis->hDC, hOldPen);
    DeleteObject(hPen);
}

Illustration

樱花细雨 2024-11-23 07:15:03

如果你使用 python 那么这可能会有所帮助......


def rgb(p):# <-- percentage as parameter
    #Starting with color red
    d = [255,0,0]
    #formula for finding green value by percentage
    d[1] = int((510*p)/100)
    print(d[1])
    #if green value more than 255
    #set green value 255
    #reduce the red value from remaining green value
    if d[1]>255:
        d[0] -= d[1]-255
        d[1] = 255
        
    return d

print(rgb(0))

If you are on python then this might help...


def rgb(p):# <-- percentage as parameter
    #Starting with color red
    d = [255,0,0]
    #formula for finding green value by percentage
    d[1] = int((510*p)/100)
    print(d[1])
    #if green value more than 255
    #set green value 255
    #reduce the red value from remaining green value
    if d[1]>255:
        d[0] -= d[1]-255
        d[1] = 255
        
    return d

print(rgb(0))
夏九 2024-11-23 07:15:03

在Javascript中生成颜色相对于速度。

最大速度 = 红色

最小速度 (0) = 绿色

var g = speed / speedMax * 100
        var color = [0, 255, 0];
        color[0] = (510 * g) / 100
        if(color[0] > 255) {
          color[1] = color[1] - (color[0] - 255)
          color[0] = 255
        }

在此处输入图像描述

代码基于 AssaultOPS 版本

In Javascript to generate the color relative to the speed.

Max speed = red

Minimum speed (0) = green

var g = speed / speedMax * 100
        var color = [0, 255, 0];
        color[0] = (510 * g) / 100
        if(color[0] > 255) {
          color[1] = color[1] - (color[0] - 255)
          color[0] = 255
        }

enter image description here

The code is based on the version of AssaultOPS

じее 2024-11-23 07:15:02

我有同样的需求,我刚刚解决了这个问题:

myColor = new Color(2.0f * x, 2.0f * (1 - x), 0);

解释:
让我们关注颜色分量的 [0.0-1.0] 范围,而不是 [0-255] 范围:

  • 绿色 = 0.0, 1.0, 0.0
  • 黄色 = 1.0, 1.0, 0.0
  • 红色 = 1.0, 0.0, 0.0

如果您只是缩放绿色分量从 0.0(一端)到 1.0(另一端),并对红色分量执行相同的操作(但向后看),你会得到丑陋且不均匀的颜色分布。

为了让它看起来漂亮,我们可以写很多代码,或者我们可以更聪明。

如果仔细观察单个分量,您会发现我们可以将范围分成两个相等的部分:在第一个分量中,我们将红色分量从 0.0 增加到 1.0,将绿色分量保留为 1.0,将蓝色分量保留为 0.0;在第二个中,我们减少绿色分量,使其他两个保持原样。我们可以利用任何高于 1.0 的值都将被读取为 1.0 的事实,通过最大化我们的值来简化代码。假设您的 x 值从 0.00 (0%) 到 1.00 (100%),您可以将其乘以 2,使其超过颜色分量 1.0 的限制。现在,您的组件从 0.0 变为 2.0(红色),从 2.0 变为 0.0(绿色)。让它们被剪裁到 [0.0-1.0] 范围,然后就可以了。

如果你的 x 在另一个范围内移动(比如 [0-100]),你需要选择一个合适的因子而不是 2

I had the same need and I just resolved with this:

myColor = new Color(2.0f * x, 2.0f * (1 - x), 0);

Explanation:
Instead of the [0-255] range, let's focus on the [0.0-1.0] range for color components:

  • Green = 0.0, 1.0, 0.0
  • Yellow = 1.0, 1.0, 0.0
  • Red= 1.0, 0.0, 0.0

If you just scale the green component from 0.0 (on one end) to 1.0 (on the other end) and do the same thing with the red component (but going backwards), you'll get ugly and non-uniform color distribution.

To make it look nice, we could write a lot of code, or we could be more clever.

If you look carefully at the single components, you can see that we can split the range in two equal parts: in the first one we increase the red component from 0.0 to 1.0, leaving the green at 1.0 and the blue at 0.0; in the second we decrease the green component, leaving the other 2 as they are. We can take advantage of the fact that any value above 1.0 will be read as 1.0, by maxing out our values to simplify the code. Assuming your x value goes from 0.00 (0%) to 1.00 (100%), you can multiply it by 2 to let it go over the 1.0 limit for color components. Now you have your components going from 0.0 to 2.0 (the red one) and from 2.0 to 0.0 (the green one). Let them be clipped to [0.0-1.0] ranges and there you go.

If your x moves in another range (like [0-100]) you need to choose an appropriate factor instead of 2

如梦亦如幻 2024-11-23 07:15:02

颜色的 RGB 值:

  • 红色 255, 0, 0
  • 黄色 255, 255, 0
  • 绿色 0, 255, 0

在红色和黄色之间,等间隔添加到绿色通道,直到达到 255。在黄色和绿色之间,等间隔你从红色通道中减去的部分。

The RGB values for the colors:

  • Red 255, 0, 0
  • Yellow 255, 255, 0
  • Green 0, 255, 0

Between Red and Yellow, equally space your additions to the green channel until it reaches 255. Between Yellow and Green, equally space your subtractions from the red channel.

却一份温柔 2024-11-23 07:15:02

这是颜色分量的非常简单的线性插值。它可能会满足您的需求。

public Color GetBlendedColor(int percentage)
{
    if (percentage < 50)
        return Interpolate(Color.Red, Color.Yellow, percentage / 50.0);
    return Interpolate(Color.Yellow, Color.Lime, (percentage - 50) / 50.0);
}

private Color Interpolate(Color color1, Color color2, double fraction)
{
    double r = Interpolate(color1.R, color2.R, fraction);
    double g = Interpolate(color1.G, color2.G, fraction);
    double b = Interpolate(color1.B, color2.B, fraction);
    return Color.FromArgb((int)Math.Round(r), (int)Math.Round(g), (int)Math.Round(b));
}

private double Interpolate(double d1, double d2, double fraction)
{
    return d1 + (d2 - d1) * fraction;
}

Here is a very simple linear interpolation of the color components. It might serve your needs.

public Color GetBlendedColor(int percentage)
{
    if (percentage < 50)
        return Interpolate(Color.Red, Color.Yellow, percentage / 50.0);
    return Interpolate(Color.Yellow, Color.Lime, (percentage - 50) / 50.0);
}

private Color Interpolate(Color color1, Color color2, double fraction)
{
    double r = Interpolate(color1.R, color2.R, fraction);
    double g = Interpolate(color1.G, color2.G, fraction);
    double b = Interpolate(color1.B, color2.B, fraction);
    return Color.FromArgb((int)Math.Round(r), (int)Math.Round(g), (int)Math.Round(b));
}

private double Interpolate(double d1, double d2, double fraction)
{
    return d1 + (d2 - d1) * fraction;
}
小…红帽 2024-11-23 07:15:02

我不懂 C#,所以这个答案只是建议的方法。令x 表示范围从0100int。像这样的东西应该可以工作:

red   = (x > 50 ? 1-2*(x-50)/100.0 : 1.0);
green = (x > 50 ? 1.0 : 2*x/100.0);
blue  = 0.0

想法是从红色开始:(1.0,0.0,0.0)。然后增加绿色以获得黄色:(1.0,1.0,0.0)。然后减少红色变为绿色:(0.0,1.0,0.0)

编辑:这是 C# 中的代码

static Color GetColorFromRedYellowGreenGradient(double percentage)
{
    var red = (percentage > 50 ? 1 - 2 * (percentage - 50) / 100.0 : 1.0) * 255;
    var green = (percentage > 50 ? 1.0 : 2 * percentage / 100.0) * 255;
    var blue = 0.0;
    Color result = Color.FromArgb((int)red, (int)green, (int)blue);
    return result;
}

I don't know C#, so this answer is just a suggested approach. Let x denote the int that ranges from 0 to 100. Something like this should work:

red   = (x > 50 ? 1-2*(x-50)/100.0 : 1.0);
green = (x > 50 ? 1.0 : 2*x/100.0);
blue  = 0.0

The idea is to start at red: (1.0,0.0,0.0). Then increase the green to get yellow: (1.0,1.0,0.0). Then decrease the red to get green: (0.0,1.0,0.0).

Edit: Here is the code in C#

static Color GetColorFromRedYellowGreenGradient(double percentage)
{
    var red = (percentage > 50 ? 1 - 2 * (percentage - 50) / 100.0 : 1.0) * 255;
    var green = (percentage > 50 ? 1.0 : 2 * percentage / 100.0) * 255;
    var blue = 0.0;
    Color result = Color.FromArgb((int)red, (int)green, (int)blue);
    return result;
}
笑着哭最痛 2024-11-23 07:15:02

简化的扩展方法;

public static Color Interpolate(this Color source, Color target, double percent)
{
    var r = (byte)(source.R + (target.R - source.R) * percent);
    var g = (byte)(source.G + (target.G - source.G) * percent);
    var b = (byte)(source.B + (target.B - source.B) * percent);

    return Color.FromArgb(255, r, g, b);
}

用法;

var low = 33.0;
var high = 100.0;
var color = Color.Red.Interpolate(Color.Green, low / high);

Simplified extension method;

public static Color Interpolate(this Color source, Color target, double percent)
{
    var r = (byte)(source.R + (target.R - source.R) * percent);
    var g = (byte)(source.G + (target.G - source.G) * percent);
    var b = (byte)(source.B + (target.B - source.B) * percent);

    return Color.FromArgb(255, r, g, b);
}

Usage;

var low = 33.0;
var high = 100.0;
var color = Color.Red.Interpolate(Color.Green, low / high);
活泼老夫 2024-11-23 07:15:02

在我尝试了一段时间更真实的颜色之后,这是我的公式:

public Color GetColorOf(double value, double minValue, double maxValue)
{
    if (value == 0 || minValue == maxValue) return Color.White;

    var g = (int)(240 * value / maxValue);
    var r = (int)(240 * value / minValue);

    return (value > 0
        ? Color.FromArgb(240 - g, 255 - (int)(g * ((255 - 155) / 240.0)), 240 - g)
        : Color.FromArgb(255 - (int)(r * ((255 - 230) / 240.0)), 240 - r, 240 - r));
}
  • 对于 0(或 NULL),或者当 min = max 时,您不会得到背景(即 Color.White)。
  • 对于所有正值,您将获得在 RGB(240, 255, 240) 和 RGB(0, 155, 0) 之间均匀分布的绿色。
  • 对于所有负值,您将获得在 RGB(255, 240, 240) 和 RGB(230, 0, 0) 之间均匀分布的红色。

热图

After I experimented for a while with more realistic colors, here is my formula:

public Color GetColorOf(double value, double minValue, double maxValue)
{
    if (value == 0 || minValue == maxValue) return Color.White;

    var g = (int)(240 * value / maxValue);
    var r = (int)(240 * value / minValue);

    return (value > 0
        ? Color.FromArgb(240 - g, 255 - (int)(g * ((255 - 155) / 240.0)), 240 - g)
        : Color.FromArgb(255 - (int)(r * ((255 - 230) / 240.0)), 240 - r, 240 - r));
}
  • You get no background (i.e. Color.White) for 0 (or NULL), or when min = max.
  • For all positive values, you get an evenly distributed green color between RGB(240, 255, 240) and RGB(0, 155, 0).
  • For all negative values, you get an evenly distributed red color between RGB(255, 240, 240) and RGB(230, 0, 0).

heat map

小忆控 2024-11-23 07:15:02

您需要使用 HSB 或 HSV 颜色表示,并使用 H(“Hue “) 价值。请参阅有关 RGB 和 HSB/HSV 之间转换的其他 SO 问题:如何更改 RGB颜色到 HSV?

You need to use the HSB or HSV color representation instead, and play with the H ("Hue") value. See this other SO question for transformation betweeen RGB and HSB/HSV: How to change RGB color to HSV?

梦罢 2024-11-23 07:15:02

此方法(在 C# 中,但可以轻松翻译为其他语言)将采用百分比和颜色列表,并根据百分比返回渐变上的颜色。当您传递颜色时,它们需要按从 0 值到 100 值的顺序排列(因此您需要按绿色、黄色、红色的顺序传递)。如果您发现中间需要不同或更多颜色,只需按照您希望它们出现的顺序将它们添加到您传递的颜色列表中即可。

public Color ColorBasedOnPercent(decimal percent, params Color[] colors)
    {
        if (colors.Length == 0)
        {
            //I am using Transparent as my default color if nothing was passed
            return Color.Transparent;
        }
        if (percent > 1)
        {
            percent = percent / 100;
        }

        //find the two colors within your list of colors that the percent should fall between
        var colorRangeIndex = (colors.Length - 1) * percent;
        var minColorIndex = (int)Math.Truncate(colorRangeIndex);
        var maxColorIndex = minColorIndex + 1;
        var minColor = colors[minColorIndex];

        if (maxColorIndex < colors.Length)
        {
            var maxColor = colors[maxColorIndex];

            //get the differences between all the color values for the two colors you are fading between
            var aScale = maxColor.A - minColor.A;
            var redScale = maxColor.R - minColor.R;
            var greenScale = maxColor.G - minColor.G;
            var blueScale = maxColor.B - minColor.B;

            //the decimal distance of how "far" this color should be from the minColor in the range
            var gradientPct = colorRangeIndex - minColorIndex;

            //for each piece of the color (ARGB), add a percentage(gradientPct) of the distance between the two colors
            int getRGB(int originalRGB, int scale) => (int)Math.Round(originalRGB + (scale * gradientPct));

            return Color.FromArgb(getRGB(minColor.A, aScale), getRGB(minColor.R, redScale), getRGB(minColor.G, greenScale), getRGB(minColor.B, blueScale));
        }
        return minColor;
    }

This method (in c#, but can be easily translated to other languages) will take a percentage and list of colors and return the color on the gradient based on your percentage. When you pass in the colors, they need to be in order from 0 value to 100 value (so you would want to pass Green, Yellow, Red - in that order). If you find that you want different or more colors in the middle, just add them to the list of colors you pass in the order you want them to appear.

public Color ColorBasedOnPercent(decimal percent, params Color[] colors)
    {
        if (colors.Length == 0)
        {
            //I am using Transparent as my default color if nothing was passed
            return Color.Transparent;
        }
        if (percent > 1)
        {
            percent = percent / 100;
        }

        //find the two colors within your list of colors that the percent should fall between
        var colorRangeIndex = (colors.Length - 1) * percent;
        var minColorIndex = (int)Math.Truncate(colorRangeIndex);
        var maxColorIndex = minColorIndex + 1;
        var minColor = colors[minColorIndex];

        if (maxColorIndex < colors.Length)
        {
            var maxColor = colors[maxColorIndex];

            //get the differences between all the color values for the two colors you are fading between
            var aScale = maxColor.A - minColor.A;
            var redScale = maxColor.R - minColor.R;
            var greenScale = maxColor.G - minColor.G;
            var blueScale = maxColor.B - minColor.B;

            //the decimal distance of how "far" this color should be from the minColor in the range
            var gradientPct = colorRangeIndex - minColorIndex;

            //for each piece of the color (ARGB), add a percentage(gradientPct) of the distance between the two colors
            int getRGB(int originalRGB, int scale) => (int)Math.Round(originalRGB + (scale * gradientPct));

            return Color.FromArgb(getRGB(minColor.A, aScale), getRGB(minColor.R, redScale), getRGB(minColor.G, greenScale), getRGB(minColor.B, blueScale));
        }
        return minColor;
    }
半岛未凉 2024-11-23 07:15:02

看一下 LinearGradientBrush 。它应该是您正在寻找的内容的完整实现。

Take a look at LinearGradientBrush. It should be a complete implementation on what you're looking for.

弥枳 2024-11-23 07:15:02

我为 UIColor 编写了一个 Swift 4 扩展,它将百分比 (0-100) 转换为从红色到绿色的 UIColor。

我目前正在将它用作分析应用程序中的进度条。这是一个例子:

let myNewColor = UIColor().greenRedProgress(percent: Int))

这是扩展:

extension UIColor{

func greenRedProgress(percent: Int) -> UIColor{
    let modVal = CGFloat((Double(percent).truncatingRemainder(dividingBy: 50) / 50) * 255)
    if percent <= 0{
        return UIColor(red: 1.0, green: 0, blue: 0, alpha: 1)
    }else if percent >= 100{
        return UIColor(red: 0, green: 1.0, blue: 0, alpha: 1)
    }else{
        switch percent{
            case 1..<50: return UIColor(red: 1.0, green: (modVal/255), blue: 0, alpha: 1)
            case 51..<100: return UIColor(red: (255 - modVal)/255, green: 1.0, blue: 0, alpha: 1)
            case 50: return UIColor(red: 1.0, green: 1.0, blue: 0, alpha: 1)
            default: return UIColor(red: 0, green: 1.0, blue: 0, alpha: 1)
        }
    }
}}

I wrote a Swift 4 extension for UIColor which converts a percentage (0-100) to a UIColor from red to green.

I'm currently using it for progress bars in an analytics app. Here's an example:

let myNewColor = UIColor().greenRedProgress(percent: Int))

Here's the extension:

extension UIColor{

func greenRedProgress(percent: Int) -> UIColor{
    let modVal = CGFloat((Double(percent).truncatingRemainder(dividingBy: 50) / 50) * 255)
    if percent <= 0{
        return UIColor(red: 1.0, green: 0, blue: 0, alpha: 1)
    }else if percent >= 100{
        return UIColor(red: 0, green: 1.0, blue: 0, alpha: 1)
    }else{
        switch percent{
            case 1..<50: return UIColor(red: 1.0, green: (modVal/255), blue: 0, alpha: 1)
            case 51..<100: return UIColor(red: (255 - modVal)/255, green: 1.0, blue: 0, alpha: 1)
            case 50: return UIColor(red: 1.0, green: 1.0, blue: 0, alpha: 1)
            default: return UIColor(red: 0, green: 1.0, blue: 0, alpha: 1)
        }
    }
}}
给不了的爱 2024-11-23 07:15:02

你只需要创建一个带有整数参数的函数

  • input 100 将返回 RGB (100, 0, 0)
  • input 50 将返回 RGB (50, 50, 0)
  • input 0 将返回 RGB (0, 100, 0)
  • input 99 将返回RGB (99, 1, 0)
  • 输入 98 将返回 RGB (98, 2, 0)
  • 输入 2 将返回 RGB (2, 98, 0)
  • 输入 1 将返回 RGB (1, 99, 0)

     私有颜色推子(int v){
           返回 Color.FromArgb(v, 100-v, 0); 
        }
    

you just need to create a function with integer parameter

  • input 100 will return RGB (100, 0, 0)
  • input 50 will return RGB (50, 50, 0)
  • input 0 will return RGB (0, 100, 0)
  • input 99 will return RGB (99, 1, 0)
  • input 98 will return RGB (98, 2, 0)
  • input 2 will return RGB (2, 98, 0)
  • input 1 will return RGB (1, 99, 0)

        private Color fader(int v){
           return Color.FromArgb(v, 100-v, 0); 
        }
    
坏尐絯℡ 2024-11-23 07:15:02

我今天需要类似的东西。输入为从 0.0 到 1.0 的百分比,输出为红色到绿色。基于jterrace的答案实现:

Color percentToColor(float percent)
{
    if (percent<0 || percent>1) { return Color.Black; }

    int r, g;
    if (percent<0.5)
    {
        r=255;
        g = (int)(255*percent/0.5);  //closer to 0.5, closer to yellow (255,255,0)
    }
    else
    {
        g=255;
        r = 255 - (int)(255*(percent-0.5)/0.5); //closer to 1.0, closer to green (0,255,0)
    }
    return Color.FromArgb(r, g, 0);
}

I had a need for something similar today. Input was percent from 0.0 to 1.0, and output red to green. Implementation based on jterrace's answer:

Color percentToColor(float percent)
{
    if (percent<0 || percent>1) { return Color.Black; }

    int r, g;
    if (percent<0.5)
    {
        r=255;
        g = (int)(255*percent/0.5);  //closer to 0.5, closer to yellow (255,255,0)
    }
    else
    {
        g=255;
        r = 255 - (int)(255*(percent-0.5)/0.5); //closer to 1.0, closer to green (0,255,0)
    }
    return Color.FromArgb(r, g, 0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文