我正在尝试获取用户输入的十六进制整数并使其变暗。有任何想法吗?
问问题
1766 次
3 回答
2
只需进行二进制减法:
int red = 0xff0000;
int darkerRed = (0xff0000 - 0x110000);
int programmaticallyDarkerRed;
您甚至可以使用循环使其更暗:
for(int i = 1; i < 15; i++)
{
programmaticallyDarkerRed = (0xff0000 - (i * 0x110000));
}
它越接近0x000000
或变黑,它就会越暗。
于 2012-08-01T23:26:22.033 回答
1
我会用Color.darker
.
Color c = Color.decode(hex).darker();
于 2012-08-01T23:22:58.053 回答
0
您可以将十六进制值转换为 aColor
然后使Color
对象变暗
// Convert the hex to an color (or use what ever method you want)
Color color = Color.decode(hex);
// The fraction of darkness you want to apply
float fraction = 0.1f;
// Break the color up
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int alpha = color.getAlpha();
// Convert to hsb
float[] hsb = Color.RGBtoHSB(red, green, blue, null);
// Decrease the brightness
hsb[2] = Math.min(1f, hsb[2] * (1f - fraction));
// Re-assemble the color
Color hSBColor = Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
// If you need it, you will need to reapply the alpha your self
更新
要将其恢复为十六进制,您可以尝试类似
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
String rHex = Integer.toString(r, 16);
String gHex = Integer.toString(g, 16);
String bHex = Integer.toString(b, 16);
String hexValue = (rHex.length() == 2 ? "" + rHex : "0" + rHex)
+ (gHex.length() == 2 ? "" + gHex : "0" + gHex)
+ (bHex.length() == 2 ? "" + bHex : "0" + bHex);
int intValue = Integer.parseInt(hex, 16);
现在,如果那不太正确,我会看看是否有任何 SO 或 Google 的答案
于 2012-08-01T23:10:37.850 回答