简体   繁体   中英

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

Can any one tell me how can I do same as 0to255 in a Windows Store app (C#)?

In short I need a logic in which I will pass a color & it will give me a list of color.

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

GetColorCollection(510099) should give me the result same as this .

The website you linked to uses HSL (Hue Saturation Lightness) instead of RGB to represent the colors, and then varies the Lightness from 0 to 255 (hence the name).

The Color class provides getBrightness() , but nothing to manipulate the Lightness directly. Luckily, you're not the first person to need that conversion - you can just use the ColorRGB class from this answer .

I struggled with this one, and I can't get the right values (same as the website) but, I think this is pretty close. I hope this is useful to you.

I tested it with the value: 15bee1

http://0to255.com/15bee1

And these are my results:

梯度为0到255

I don't like to create functions that has strings as input instead when colors meant. So I made some conversion.

And this is the code:

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

And this is how to call it:

string hexcolor = "15bee1";

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

I didn't test this, but should work.

First, get a System.Drawing.Color from hexcode using ColorTranslator

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

Then, use ControlPaint.Light and ControlPaint.Dark to adjust the brightness of the color

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

Finally, convert each color in the list back to hex

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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