簡體   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