C#: Create a lighter/darker color based on a system color

Duplicate

How do I adjust the brightness of a color?
How do I determine darker or lighter color variant of a given color?
Programmatically Lighten a Color


Say I have

var c = Color.Red;

Now I want to create a new Color that is lighter or darker than that color. How can I do that without too much hassle?


ControlPaint.Light .Dark .DarkDark等

Color lightRed = ControlPaint.Light( Color.Red );

I recently blogged about this . The main idea is to apply a given correction factor to each of the color components. The following static method modifies the brightness of a given color with a specified correction factor and produces a darker or a lighter variant of that color:

/// <summary>
/// Creates color with corrected brightness.
/// </summary>
/// <param name="color">Color to correct.</param>
/// <param name="correctionFactor">The brightness correction factor. Must be between -1 and 1. 
/// Negative values produce darker colors.</param>
/// <returns>
/// Corrected <see cref="Color"/> structure.
/// </returns>
public static Color ChangeColorBrightness(Color color, float correctionFactor)
{
    float red = (float)color.R;
    float green = (float)color.G;
    float blue = (float)color.B;

    if (correctionFactor < 0)
    {
        correctionFactor = 1 + correctionFactor;
        red *= correctionFactor;
        green *= correctionFactor;
        blue *= correctionFactor;
    }
    else
    {
        red = (255 - red) * correctionFactor + red;
        green = (255 - green) * correctionFactor + green;
        blue = (255 - blue) * correctionFactor + blue;
    }

    return Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
}

You can also do this using a Lerp function. There's one in XNA, but it's easy to write yourself.

See my answer to this similar question for a C# implementation.

The function lets you do this:

// make red 50% lighter:
Color.Red.Lerp( Color.White, 0.5 );

// make red 75% darker:
Color.Red.Lerp( Color.Black, 0.75 );

// make white 10% bluer:
Color.White.Lerp( Color.Blue, 0.1 );
链接地址: http://www.djcxy.com/p/87398.html

上一篇: 如何确定给定颜色的较暗或较浅的颜色变体?

下一篇: C#:根据系统颜色创建更亮/更暗的颜色