C#:基于系统颜色创建更亮/更暗的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/801406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: Create a lighter/darker color based on a system color
提问by Svish
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?
现在我想创建一个Color
比该颜色更浅或更深的新颜色。我怎样才能做到这一点而不会太麻烦?
采纳答案by Paul Alexander
ControlPaint.Light .Dark .DarkDark, etc.
ControlPaint.Light .Dark .DarkDark 等
Color lightRed = ControlPaint.Light( Color.Red );
回答by Soul_Master
Using HSI converter library(search google). And then, adjust I channel for lighter/darker color.
使用 HSI 转换器库(搜索谷歌)。然后,调整 I 通道以获得更亮/更暗的颜色。
回答by Homes2001
Take a look at the ControlPaint class:
看看 ControlPaint 类:
回答by Tom Carter
Here's some javascript code I use for lightening/darkening a given colour. You could use it as a base for an equivalent C# function
这是我用于使给定颜色变亮/变暗的一些 javascript 代码。您可以将其用作等效 C# 函数的基础
It works by calculating a distance from pure white of each of the RGB components and then adjusts this distance by the provided factor. The new distance is used to calculate the new colour. A factor of between 0 and 1 darkens, a factor higher than 1 lightens
它的工作原理是计算每个 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;
}
回答by Keith
You can also do this using a Lerp
function. There's one in XNA, but it's easy to write yourself.
您也可以使用Lerp
函数执行此操作。XNA 中有一个,但是自己编写很容易。
See my answer to this similar questionfor 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 );
回答by Pavel Vladov
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);
}
回答by TugboatCaptain
Most of these methods do darken the color but they adjust the hue way to much so the result doesn't look very good. The best answer is to use Rich Newman's HSLColorclass and adjust the luminosity.
大多数这些方法确实会使颜色变暗,但它们将色调方式调整得太多,因此结果看起来不太好。最好的答案是使用Rich Newman 的 HSLColor类并调整亮度。
public Color Darken(Color color, double darkenAmount) {
HSLColor hslColor = new HSLColor(color);
hslColor.Luminosity *= darkenAmount; // 0 to 1
return hslColor;
}
回答by Zoltán Tamási
Taking the core method of @Pavel's answerI prepared the following two little extension methods for a more intuitive (at least for me) signature.
以@Pavel 回答的核心方法为基础,我准备了以下两个小扩展方法,以获得更直观(至少对我而言)的签名。
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);
}
回答by Omar Negm
You can also simply work on the RGB percentage to get it lighter or darker as you want, Here is an example for how to make a color darker x% than it is:
您还可以根据需要简单地处理 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));
}
One more thing we can also reverse the process to be lighter instead, Only we getting the result of 255 - RGB and then multiply it by the percentage we want like the following example:
还有一件事,我们也可以将过程反转为更亮,只有我们得到 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))
);
}
Hope that helps.
希望有帮助。
回答by shinra tensei
I made a site that does this colorglower.comYou can check it out to see a demo.
我制作了一个网站colorglower.com您可以查看它以查看演示。
Here's the javascript code i used.
这是我使用的 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;
}
}
What this code does is it receives a hex color and then it outputs 10 lightest color versions of it and puts in in the array. You can change the luminosity to whatever you like to adjust the shade percentage. To darken the colors you just need to change:
这段代码的作用是接收一个十六进制颜色,然后输出它的 10 个最亮的颜色版本并放入数组中。您可以将亮度更改为您喜欢的任何值来调整阴影百分比。要使颜色变暗,您只需要更改:
luminosity -= calcPercentage();