简体   繁体   English

Android 改变颜色的亮度

[英]Android changing a color's brightness

I want to change the brightness of any given color (Note: I am not talking about screen brightness), I have looked at the Color class, it has a few methods for conversions between RGB and HSV, I'm a newbie in this area.我想改变任何给定颜色的亮度(注意:我不是在谈论屏幕亮度),我看过Color class,它有几种在 RGB 和 HSV 之间转换的方法,我是这个领域的新手. To start with, how do I change the brightness of red, if its value is spefied in RGB (#FF0000)?首先,如果红色的值以 RGB (#FF0000) 为单位指定,我该如何更改红色的亮度?

最简单的方法是将颜色转换为HSL(不是HSV!它们是不同的-请参阅http://en.wikipedia.org/wiki/HSL_and_HSV )并更改L分量-增加使其更亮,减少使其更暗。

Considering that you are talking about brightness (color enhance) and not luminance (white amount), your model is the HSV (aka HSB) and not HSL. 考虑到您是在谈论亮度 (色彩增强)而不是亮度 (白色数量),因此您的模型是HSV(aka HSB)而不是HSL。

On fast briefing, if you enhance the V channel on HSV over, lets say... some blue, you have a "more blue" color. 在快速简报中,如果您将HSV上的V通道增强了,可以说...有些蓝色,则您的颜色为“更多蓝色”。 If you enhance the L channel on HSL model you have a more "clear and washed" blue. 如果您在HSL模型上增强了L通道,则您会看到一个更加“清晰和水洗”的蓝色。

The android.graphics.Color class have built-in support to HSV model. android.graphics.Color类具有对HSV模型的内置支持。 Use Color.colorToHSV() and Color.HSVToColor() to edit the brightness value (or hue, or saturation, if you like). 使用Color.colorToHSV()Color.HSVToColor()编辑亮度值(如果需要,还可以设置色相或饱和度)。

On HSV model, H (hue) define the base color, S (saturation) control the amount of gray and V controls the brightness. 在HSV模型上,H(色相)定义基色,S(饱和度)控制灰色量,而V控制亮度。 So, if you enhance V and decrease S at same time, you gets more luminance, in pratice. 因此,如果同时提高V值和降低S值,您将获得更多的亮度。

For starters, you need to remember two things - 对于初学者,您需要记住两件事-

  1. To reduce brightness, you can change red from #FF0000 to #AA0000 or #880000 - basically reduce the Red component. 要降低亮度,您可以将红色从#FF0000更改为#AA0000或#880000-基本减少红色分量。
  2. You can also try reducing opacity - often you'll realize that it works better than just reducing brightness. 您还可以尝试降低不透明度-通常您会发现它的效果比仅降低亮度更好。

You can use Color.colorToHSV to convert the color to HSV, then change the brightness of the HSV color, then use Color.HSVToColor to convert it back to a color int.您可以使用Color.colorToHSV将颜色转换为 HSV,然后更改 HSV 颜色的亮度,然后使用Color.HSVToColor将其转换回颜色 int。 For example, the following code sets the brightness to 0.5:例如,以下代码将亮度设置为 0.5:

@ColorInt int originalColor = /*your original color*/;
float[] hsv = new float[3];    //Create an array to pass to the colorToHSV function
Color.colorToHSV(originalColor, hsv);    //Put the HSV components in the array created above
hsv[2] = 0.5f;    //Whatever brightness you want to set. 0 is black, 1 is the pure color.
@ColorInt int newColor = Color.HSVToColor(hsv);    //Convert it back to a ColorInt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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