如何使给定颜色变暗(int)
How to darken a given color (int)
我有一个采用给定颜色的函数,我希望它能使颜色变暗(将亮度降低 20% 左右)。仅给定一种颜色(int),我无法弄清楚如何做到这一点。什么是正确的方法?
public static int returnDarkerColor(int color){
int darkerColor = ....
return darkerColor;
}
将颜色转换为 HSV array,然后将亮度降低 20%,然后使用 HSVToColor
将 HSV 数组转换回 RGB。注意:您要在数组中更改的值将是 V
值。 (即 hsv[2]
)
如果你想要更简单而不是准确,下面的内容可能对你有帮助。
public static int returnDarkerColor(int color){
float ratio = 1.0f - 0.2f;
int a = (color >> 24) & 0xFF;
int r = (int) (((color >> 16) & 0xFF) * ratio);
int g = (int) (((color >> 8) & 0xFF) * ratio);
int b = (int) ((color & 0xFF) * ratio);
return (a << 24) | (r << 16) | (g << 8) | b;
}
更Android的方法:
public static int manipulateColor(int color, float factor) {
int a = Color.alpha(color);
int r = Math.round(Color.red(color) * factor);
int g = Math.round(Color.green(color) * factor);
int b = Math.round(Color.blue(color) * factor);
return Color.argb(a,
Math.min(r,255),
Math.min(g,255),
Math.min(b,255));
}
您需要使用小于 1.0f
的系数来加深颜色。试试 0.8f
.
建议使用像 RogueBaneling 这样的 HSV 的更简单的解决方案:
Kotlin
@ColorInt fun darkenColor(@ColorInt color: Int): Int {
return Color.HSVToColor(FloatArray(3).apply {
Color.colorToHSV(color, this)
this[2] *= 0.8f
})
}
Java
@ColorInt int darkenColor(@ColorInt int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f;
return Color.HSVToColor(hsv);
}
不需要复杂的按位运算或 Math
。如果需要,将 0.8f
移至参数。
还有一个以前没有提到的更简单的解决方案,Android 有一个名为 ColorUtils
的 class,您可以帮助您解决这个问题
这是扩展函数的 Kotlin 片段
inline val @receiver:ColorInt Int.darken
@ColorInt
get() = ColorUtils.blendARGB(this, Color.BLACK, 0.2f)
方法 ColorUtils.blendARGB
将您的颜色作为第一个参数,第二个参数是您要混合的颜色,black
在这种情况下,最后一个参数是您的颜色与你正在混合的黑色。
这样您可以 select 百分比的颜色,从而获取更深或更浅的阴影
int color = ((ColorDrawable)c2.getBackground()).getColor();
int colorLighter = color - color * 40/100;
int colorDarker = color + color * 40/100;
colorlighter 使我们的按钮背景颜色更浅
colordarker 使我们的按钮背景颜色更深
最好的方法是使用 ColorUtils.blendARGB
更深:
val newColor = ColorUtils.blendARGB(this, Color.BLACK, 0.5f)
打火机:
val newColor = ColorUtils.blendARGB(this, Color.WHITE, 0.5f)
我有一个采用给定颜色的函数,我希望它能使颜色变暗(将亮度降低 20% 左右)。仅给定一种颜色(int),我无法弄清楚如何做到这一点。什么是正确的方法?
public static int returnDarkerColor(int color){
int darkerColor = ....
return darkerColor;
}
将颜色转换为 HSV array,然后将亮度降低 20%,然后使用 HSVToColor
将 HSV 数组转换回 RGB。注意:您要在数组中更改的值将是 V
值。 (即 hsv[2]
)
如果你想要更简单而不是准确,下面的内容可能对你有帮助。
public static int returnDarkerColor(int color){
float ratio = 1.0f - 0.2f;
int a = (color >> 24) & 0xFF;
int r = (int) (((color >> 16) & 0xFF) * ratio);
int g = (int) (((color >> 8) & 0xFF) * ratio);
int b = (int) ((color & 0xFF) * ratio);
return (a << 24) | (r << 16) | (g << 8) | b;
}
更Android的方法:
public static int manipulateColor(int color, float factor) {
int a = Color.alpha(color);
int r = Math.round(Color.red(color) * factor);
int g = Math.round(Color.green(color) * factor);
int b = Math.round(Color.blue(color) * factor);
return Color.argb(a,
Math.min(r,255),
Math.min(g,255),
Math.min(b,255));
}
您需要使用小于 1.0f
的系数来加深颜色。试试 0.8f
.
建议使用像 RogueBaneling 这样的 HSV 的更简单的解决方案:
Kotlin
@ColorInt fun darkenColor(@ColorInt color: Int): Int {
return Color.HSVToColor(FloatArray(3).apply {
Color.colorToHSV(color, this)
this[2] *= 0.8f
})
}
Java
@ColorInt int darkenColor(@ColorInt int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f;
return Color.HSVToColor(hsv);
}
不需要复杂的按位运算或 Math
。如果需要,将 0.8f
移至参数。
还有一个以前没有提到的更简单的解决方案,Android 有一个名为 ColorUtils
的 class,您可以帮助您解决这个问题
这是扩展函数的 Kotlin 片段
inline val @receiver:ColorInt Int.darken
@ColorInt
get() = ColorUtils.blendARGB(this, Color.BLACK, 0.2f)
方法 ColorUtils.blendARGB
将您的颜色作为第一个参数,第二个参数是您要混合的颜色,black
在这种情况下,最后一个参数是您的颜色与你正在混合的黑色。
这样您可以 select 百分比的颜色,从而获取更深或更浅的阴影
int color = ((ColorDrawable)c2.getBackground()).getColor();
int colorLighter = color - color * 40/100;
int colorDarker = color + color * 40/100;
colorlighter 使我们的按钮背景颜色更浅 colordarker 使我们的按钮背景颜色更深
最好的方法是使用 ColorUtils.blendARGB
更深:
val newColor = ColorUtils.blendARGB(this, Color.BLACK, 0.5f)
打火机:
val newColor = ColorUtils.blendARGB(this, Color.WHITE, 0.5f)