【问题标题】:find lighter and darker colors based on any color from white to black根据从白色到黑色的任何颜色找到较浅和较深的颜色
【发布时间】:2013-09-21 08:42:14
【问题描述】:

谁能告诉我如何在 Windows 应用商店应用程序 (C#) 中与 0to255 做同样的事情?

简而言之,我需要一个逻辑,其中我将传递一种颜色,它会给我一个颜色列表。

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

GetColorCollection(510099) 应该给我与this 相同的结果。

【问题讨论】:

    标签: c# windows-8 colors windows-phone-8 windows-store-apps


    【解决方案1】:

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

    Color 类提供了getBrightness(),但没有任何东西可以直接操纵 Lightness。幸运的是,您不是第一个需要这种转换的人 - 您可以使用来自 this answerColorRGB 类。

    【讨论】:

    • 你应该提到伽马校正,亮度不是线性的。
    • ColorRGB 类不适用于我。我正在为 Windows 8 和 Windows 手机创建应用程序。 System.Drawing.Color有GetHue()等方法,适用于WinForms。
    • 对不起,我没有意识到这一点。但是,找到fully usable implementation of an RGB-HSL converter 并不难。完成后,只需将其插入 ColorRGB 类中的适当位置
    【解决方案2】:

    我在这个问题上苦苦挣扎,我无法获得正确的值(与网站相同),但我认为这非常接近。 我希望这对你有用。

    我用值进行了测试:15bee1

    http://0to255.com/15bee1

    这些是我的结果:



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

    这是代码:

    // 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);
    

    【讨论】:

    【解决方案3】:

    我没有对此进行测试,但应该可以。

    首先,使用ColorTranslator从十六进制代码中获取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);
    

    【讨论】:

    • 这对我没用。我正在创建 Windows 8 和 Windows Phone 8 应用程序。结果也应该与 0to255.com 相同或接近
    猜你喜欢
    • 1970-01-01
    • 2010-09-10
    • 2023-04-03
    • 2021-11-29
    • 2014-07-06
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多