C# 不同值的色调

发布于 2024-10-20 13:27:29 字数 324 浏览 5 评论 0原文

我想要一些关于如何在 C# 中完成以下任务的建议。我想绘制一个“颜色”,确切的颜色将由属性值决定,为了这个例子,我们假设它是一个百分比。

理想情况下,我希望用户指定五种颜色。

  Negative MAX
  Negative MIN
  Even
  Positive MIN
  Positive MAX

用户仅指定每个级别的颜色,而不是确定最小和最大的值。

使用这些数据,我希望能够根据百分比计算颜色。即 57% 将导致色调介于 Positive MIN 和 Positive MAX 之间。如有任何帮助或建议,我们将不胜感激。

I would like some advice on how to accomplish the following within C#. I would like to plot a "color", the exact color will be determined by a property value, for the sake of this example let's assume it's a percentage.

Ideally I'd like the user to specify five colors.

  Negative MAX
  Negative MIN
  Even
  Positive MIN
  Positive MAX

The user is only specifying colors for each level, not the value which determines Min and Max.

Using this data, I would like to be able to calculate a color based on a percentage. i.e. 57% would result in a color hue in between Positive MIN and Positive MAX. Any help or advice for this is appreciated.

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

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

发布评论

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

评论(2

梦在深巷 2024-10-27 13:27:29

要在两种颜色之间进行插值,您只需对每个颜色通道进行插值。

Color color1 = Color.Red;
Color color2 = Color.Green;
double fraction = 0.3;
Color color3 = Color.FromArgb(
    (int)(color1.R * fraction + color2.R * (1 - fraction)),
    (int)(color1.G * fraction + color2.G * (1 - fraction)),
    (int)(color1.B * fraction + color2.B * (1 - fraction)));

要沿比例进行插值,您需要确定“步长”具有的确切百分比值,并将目标值投影为两个颜色步长之间的分数。

To interpolate between two colors you just interpolate each color channel

Color color1 = Color.Red;
Color color2 = Color.Green;
double fraction = 0.3;
Color color3 = Color.FromArgb(
    (int)(color1.R * fraction + color2.R * (1 - fraction)),
    (int)(color1.G * fraction + color2.G * (1 - fraction)),
    (int)(color1.B * fraction + color2.B * (1 - fraction)));

To interpolate along your scale you need to decide what exact percentage values your "steps" have and project your target value that a fraction between two of those color steps.

空心空情空意 2024-10-27 13:27:29

使用复制的函数从这里< /a>,您可以将色调转换回 RGB 颜色。该函数假设所有输入和输出的比例为 0..1,因此您需要将 color.GetHue() 除以 255。该函数下面是一些使用一组百分比的示例代码;在您选择的 UI 中实现 DisplayColor 以查看结果。

public static void HSVToRGB(double H, double S, double V, out double R, out double G, out double B)
{
    if (H == 1.0)
    {
        H = 0.0;
    }

    double step = 1.0/6.0;
    double vh = H/step;

    int i = (int) System.Math.Floor(vh);

    double f = vh - i;
    double p = V*(1.0 - S);
    double q = V*(1.0 - (S*f));
    double t = V*(1.0 - (S*(1.0 - f)));

    switch (i)
    {
        case 0:
            {
                R = V;
                G = t;
                B = p;
                break;
            }
        case 1:
            {
                R = q;
                G = V;
                B = p;
                break;
            }
        case 2:
            {
                R = p;
                G = V;
                B = t;
                break;
            }
        case 3:
            {
                R = p;
                G = q;
                B = V;
                break;
            }
        case 4:
            {
                R = t;
                G = p;
                B = V;
                break;
            }
        case 5:
            {
                R = V;
                G = p;
                B = q;
                break;
            }
        default: 
            {
                // not possible - if we get here it is an internal error
                throw new ArgumentException();
            }
    }
}

Color min = Color.Blue;
Color max = Color.Green;

float minHue = min.GetHue() / 255;
float maxHue = max.GetHue() / 255;

float scale = maxHue - minHue;

float[] percents = new float[] { .05F, .15F, .40F, .70F, .85F, .95F };

foreach (var pct in percents) {
    float curHue = minHue + (scale * pct);
    double r, g, b;
    HSVToRGB(curHue, 1.0, 1.0, out r, out g, out b);

    Color curColor = Color.FromArgb((int)(r * 255), (int)(g * 255), (int)(b * 255));
    DisplayColor(curColor);
}

Using the function copied from here, you can convert the hues back into RGB colors. This function assumes has all inputs and outputs in on a 0..1 scale, so you'll need to divide by color.GetHue() by 255. Below the function is some sample code using a set of percentages; implement DisplayColor in the UI of your choice to see the results.

public static void HSVToRGB(double H, double S, double V, out double R, out double G, out double B)
{
    if (H == 1.0)
    {
        H = 0.0;
    }

    double step = 1.0/6.0;
    double vh = H/step;

    int i = (int) System.Math.Floor(vh);

    double f = vh - i;
    double p = V*(1.0 - S);
    double q = V*(1.0 - (S*f));
    double t = V*(1.0 - (S*(1.0 - f)));

    switch (i)
    {
        case 0:
            {
                R = V;
                G = t;
                B = p;
                break;
            }
        case 1:
            {
                R = q;
                G = V;
                B = p;
                break;
            }
        case 2:
            {
                R = p;
                G = V;
                B = t;
                break;
            }
        case 3:
            {
                R = p;
                G = q;
                B = V;
                break;
            }
        case 4:
            {
                R = t;
                G = p;
                B = V;
                break;
            }
        case 5:
            {
                R = V;
                G = p;
                B = q;
                break;
            }
        default: 
            {
                // not possible - if we get here it is an internal error
                throw new ArgumentException();
            }
    }
}

Color min = Color.Blue;
Color max = Color.Green;

float minHue = min.GetHue() / 255;
float maxHue = max.GetHue() / 255;

float scale = maxHue - minHue;

float[] percents = new float[] { .05F, .15F, .40F, .70F, .85F, .95F };

foreach (var pct in percents) {
    float curHue = minHue + (scale * pct);
    double r, g, b;
    HSVToRGB(curHue, 1.0, 1.0, out r, out g, out b);

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