复制
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
说我有
var c = Color.Red;
现在我想创建一个比该颜色更亮或更暗的新Color
。如果没有太多麻烦,我怎么能这样做?
答案 0 :(得分:94)
ControlPaint。光.Dark .DarkDark等。
Color lightRed = ControlPaint.Light( Color.Red );
答案 1 :(得分:71)
我最近blogged about this。主要思想是将给定的校正因子应用于每个颜色分量。以下静态方法使用指定的校正因子修改给定颜色的亮度,并生成该颜色的较暗或较浅的变体:
/// <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);
}
答案 2 :(得分:13)
您也可以使用Lerp
功能执行此操作。 XNA中有一个,但你自己写的很容易。
有关C#实现,请参阅my answer to this similar question。
该功能可让您这样做:
// 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 );
答案 3 :(得分:8)
这些方法中的大多数都会使颜色变暗,但是它们会调整色调方式,因此结果看起来并不是很好。最好的答案是使用Rich Newman's HSLColor类并调整亮度。
public Color Darken(Color color, double darkenAmount) {
HSLColor hslColor = new HSLColor(color);
hslColor.Luminosity *= darkenAmount; // 0 to 1
return hslColor;
}
答案 4 :(得分:4)
这是我使用的一些javascript代码,用于使给定颜色变亮/变暗。您可以将它用作等效C#函数的基础
它的工作原理是计算每个RGB分量的纯白色距离,然后用提供的系数调整该距离。新距离用于计算新颜色。 0到1之间的因子变暗,高于1的因子变亮
function Darken( hexColor, factor )
{
if ( factor < 0 ) factor = 0;
var c = hexColor;
if ( c.substr(0,1) == "#" )
{
c = c.substring(1);
}
if ( c.length == 3 || c.length == 6 )
{
var i = c.length / 3;
var f; // the relative distance from white
var r = parseInt( c.substr(0, i ), 16 );
f = ( factor * r / (256-r) );
r = Math.floor((256 * f) / (f+1));
r = r.toString(16);
if ( r.length == 1 ) r = "0" + r;
var g = parseInt( c.substr(i, i), 16);
f = ( factor * g / (256-g) );
g = Math.floor((256 * f) / (f+1));
g = g.toString(16);
if ( g.length == 1 ) g = "0" + g;
var b = parseInt( c.substr( 2*i, i),16 );
f = ( factor * b / (256-b) );
b = Math.floor((256 * f) / (f+1));
b = b.toString(16);
if ( b.length == 1 ) b = "0" + b;
c = r+g+b;
}
return "#" + c;
}
答案 5 :(得分:4)
采用@Pavel's answer的核心方法我准备了以下两个小扩展方法,以便更直观(至少对我来说)签名。
public static Color LightenBy(this Color color, int percent)
{
return ChangeColorBrightness(color, percent/100.0);
}
public static Color DarkenBy(this Color color, int percent)
{
return ChangeColorBrightness(color, -1 * percent / 100.0);
}
答案 6 :(得分:2)
我改变了Pavel Vladov功能来修改甚至RGB分量,以获得任何R / G / B方向组合的阴影:
Public Function ChangeColorShades(color As Color, correctionFactor As Single, bR As Boolean, bG As Boolean, bB As Boolean) As Color
Dim red As Single = CSng(color.R)
Dim green As Single = CSng(color.G)
Dim blue As Single = CSng(color.B)
If (correctionFactor < 0) Then
correctionFactor = 1 + correctionFactor
If bR Then
red *= correctionFactor
End If
If bG Then
green *= correctionFactor
End If
If bB Then
blue *= correctionFactor
End If
Else
If bR Then
red = (255 - red) * correctionFactor + red
End If
If bG Then
green = (255 - green) * correctionFactor + green
End If
If bB Then
blue = (255 - blue) * correctionFactor + blue
End If
End If
Return color.FromArgb(color.A, CInt(red), CInt(green), CInt(blue))
End Function
答案 7 :(得分:1)
您也可以根据需要简单地处理RGB百分比以使其更亮或更暗。下面是一个如何使颜色比x更暗的示例:
//_correctionfactory in percentage, e.g 50 = make it darker 50%
private Color DarkerColor(Color color, float correctionfactory = 50f)
{
const float hundredpercent = 100f;
return Color.FromArgb((int)(((float)color.R / hundredpercent) * correctionfactory),
(int)(((float)color.G / hundredpercent) * correctionfactory), (int)(((float)color.B / hundredpercent) * correctionfactory));
}
我们还可以将该过程反转为更轻松,只有我们得到255的结果 - RGB然后乘以我们想要的百分比,如下例所示:
private Color LighterColor(Color color, float correctionfactory = 50f)
{
correctionfactory = correctionfactory / 100f;
const float rgb255 = 255f;
return Color.FromArgb((int)((float)color.R + ((rgb255 - (float)color.R) * correctionfactory)), (int)((float)color.G + ((rgb255 - (float)color.G) * correctionfactory)), (int)((float)color.B + ((rgb255 - (float)color.B) * correctionfactory))
);
}
希望有所帮助。
答案 8 :(得分:0)
使用HSI转换器库(搜索谷歌)。然后,调整I通道以获得更亮/更暗的颜色。
答案 9 :(得分:0)
看一下ControlPaint类:
答案 10 :(得分:0)
我创建了一个执行此操作的网站colorglower.com您可以查看它以查看演示。
这是我使用的javascript代码。
function lighten(color) {
// convert to decimal and change luminosity
var luminosity = 0.01
var computedColors = new Array();
var newColor = "#",
c, i, n, black = 0,
white = 255;
for (n = 0; n < 10; n++) {
for (i = 0; i < 3; i++) {
c = parseInt(color.substr(i * 2, 2), 16);
c = Math.round(Math.min(Math.max(black, c + (luminosity * white)), white)).toString(16);
newColor += ("00" + c).substr(c.length);
}
computedColors[n] = newColor;
var arrayUnique = checkIfArrayIsUnique(computedColors);
if (arrayUnique == false) {
computedColors.pop();
break;
}
computedColors[n] = newColor;
newColor = "#";
luminosity += calcPercentage();
}
return computedColors;
}
这段代码的作用是接收十六进制颜色,然后输出10个最亮的颜色版本并放入数组中。您可以将亮度更改为您喜欢的任何值,以调整阴影百分比。要使你需要改变的颜色变暗:
luminosity -= calcPercentage();