繁体   English   中英

根据从白色到黑色的任何颜色找到更浅和更深的颜色

[英]find lighter and darker colors based on any color from white to black

任何人都可以告诉我如何在Windows应用商店应用程序(C#)中执行与0to255相同的操作?

总之,我需要一个逻辑,我将传递一种颜色,它会给我一个颜色列表。

private List<string> GetColorCollection(string hexcolor)
{
    //TODO: 
}

GetColorCollection(510099)应该给我的结果一样

您链接的网站使用HSL(色调饱和度亮度)而不是RGB来表示颜色,然后将亮度从0更改为255(因此名称)。

Color类提供了getBrightness() ,但没有任何东西可以直接操作Lightness。 幸运的是,你不是第一个需要转换的人 - 你可以从这个答案中使用ColorRGB类。

我努力与这个,我无法得到正确的价值观(与网站相同),但我认为这非常接近。 我希望这对你有用。

我测试了它的值:15bee1

http://0to255.com/15bee1

这些是我的结果:

梯度为0到255

我不喜欢创建具有字符串作为输入的函数,而不是颜色。 所以我做了一些转换。

这是代码:

// convert the Color type to String notation
private string ColorToString(Color color)
{
    return String.Format("{0:x2}{1:x2}{2:x2}", color.R, color.G, color.B);
}

// convert the string notation to color type
private Color StringToColor(string hexcolor)
{
    byte r = Convert.ToByte(hexcolor.Substring(0, 2), 16);
    byte g = Convert.ToByte(hexcolor.Substring(2, 2), 16);
    byte b = Convert.ToByte(hexcolor.Substring(4, 2), 16);

    return new Color { R = r, G = g, B = b };
}

// This function calculates a gradient from <color1> to <color2> in <steps> steps
private IEnumerable<Color> GetColorGradient(Color color1, Color color2, int steps)
{
    int rD = color2.R - color1.R;
    int gD = color2.G - color1.G;
    int bD = color2.B - color1.B;

    for (int i = 1; i < steps; i++)
        yield return new Color
        {
            R = (byte)(color1.R + (rD * i / (steps))),
            G = (byte)(color1.G + (gD * i / (steps))),
            B = (byte)(color1.B + (bD * i / (steps))),
        };
}

// This will append two gradients (white->color->black)
private IEnumerable<Color> GetColorCollection(Color color, int steps)
{
    int grayValue = (color.R + color.G + color.B) / 3;

    // with the gray value I will determine the lightness of the color, so what step it should start. I don't want 16 white values, when I input a light color.
    int currentStep = (grayValue * steps / 256) - 1;

    yield return Colors.White;

    foreach (Color c in GetColorGradient(Colors.White, color, currentStep))
        yield return c;

    yield return color;

    foreach (Color c in GetColorGradient(color, Colors.Black, steps - currentStep - 1))
        yield return c;

    yield return Colors.Black;
}

// this function will convert a IEnumerable<Color> to IEnumerable<string>
private IEnumerable<string> GetColorCollection(string hexcolor, int steps)
{
    foreach (Color newColor in GetColorCollection(StringToColor(hexcolor), steps))
        yield return ColorToString(newColor);
}

这是如何称呼它:

string hexcolor = "15bee1";

IEnumerable<string> results = GetColorCollection(hexcolor, 32);

我没有测试这个,但应该工作。

首先,使用ColorTranslator从hexcode获取System.Drawing.Color

 System.Drawing.Color color = ColorTranslator.FromHtml("#FF00FF");

然后,使用ControlPaint.LightControlPaint.Dark调整颜色的亮度

List<Color> lighterColors = new List<Color>();
List<Color> darkerColors = new List<Color>();
for(int i = 0; i < 10; i++)
{
   lighterColors.Add(ControlPaint.Light(color, (float)(i / 10));
   darkerColors.Add(ControlPaint.Dark(color, (float)(i / 10));
}

最后,将列表中的每种颜色转换回十六进制

string hexcode = System.Drawing.ColorTranslator.ToHtml(color);

暂无
暂无

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

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