简体   繁体   English

如何找到介于其他两种颜色之间的颜色?

[英]How do I find a color that is between two other colors?

I am assigning each color a numeric value. 我为每种颜色分配一个数值。 For example: 例如:

Color.red: 12 
Color.Blue: 6

I need to find a color between two colors (for example, red and blue). 我需要找到两种颜色之间的颜色(例如,红色和蓝色)。 But how? 但是如何? I have tried this, 我已经试过了

(Color.red+color.blue)/2=> (12 + 6)/2 = 9

9 corresponds to Color.yellow 9对应于Color.yellow

You'll need to use the RGB values of the colour and interpolate between those. 您需要使用颜色的RGB值并在这些值之间进行插值。 Using a single value isn't going to give you the discrimination you need. 使用单个值不会给您所需的歧视。

The answer that yx quotes Drawing a line with a gradient color looks like a good place to start yx引用的答案用渐变色画一条线看起来像是一个不错的起点

Colors are normally represented as a six digit hex value for computers with Red, Green & Blue taking two digits each in that order eg FF0000 is red, 00FF00 is blue and 0000FF is Green. 对于计算机而言,颜色通常表示为六位数的十六进制值,其中红色,绿色和蓝色的顺序分别为两位数,例如FF0000为红色,00FF00为蓝色和0000FF为绿色。 You should consider how to move between those kinds of values. 您应该考虑如何在这些类型的值之间移动。

Michael Leigeber has a nice color transition algorithm in JavaScript which you can download to see how he has implemented moving between two values. Michael Leigeber 在JavaScript中有一个很好的颜色转换算法 ,您可以下载该算法以查看他如何实现在两个值之间移动。

This problem requires you to split the alpha, red, green, and blue componenets of each colour, find the average of each, and create a new color: 此问题需要您拆分每种颜色的alpha,红色,绿色和蓝色组分,找到每种的平均值,然后创建新的颜色:

    Color first = Color.Red;
    Color second = Color.Blue;


    byte r = (byte)((first.R + second.R) / (byte)2);
    byte g = (byte)((first.G + second.G) / (byte)2);
    byte b = (byte)((first.B + second.B) / (byte)2);
    byte a = (byte)((first.A + second.A) / (byte)2);

    Color mix = Color.FromArgb(a, r, g, b);

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

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