Java 需要使十六进制0xff****字符串变暗(*=任何十六进制字符)
我试图采取一个用户输入的十六进制整数,使其更暗。有什么想法吗?您可以将十六进制值转换为Java 需要使十六进制0xff****字符串变暗(*=任何十六进制字符),java,colors,rgb,Java,Colors,Rgb,我试图采取一个用户输入的十六进制整数,使其更暗。有什么想法吗?您可以将十六进制值转换为颜色,然后将颜色对象变暗 // 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 in
颜色
,然后将颜色
对象变暗
// 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
更新
要让它回到hex,你可以尝试以下方法
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的答案。你可以将十六进制值转换为
颜色,然后将颜色对象变暗
// 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
更新
要让它回到hex,你可以尝试以下方法
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的答案,我会使用
Color c=Color.decode(十六进制).darker()代码>我会使用
Color c=Color.decode(十六进制).darker()代码>只需执行二进制减法:
int red = 0xff0000;
int darkerRed = (0xff0000 - 0x110000);
int programmaticallyDarkerRed;
您甚至可以通过循环使其更暗:
for(int i = 1; i < 15; i++)
{
programmaticallyDarkerRed = (0xff0000 - (i * 0x110000));
}
for(int i=1;i<15;i++)
{
programmaticallydarkered=(0xff0000-(i*0x110000));
}
越接近0x000000
或黑色,颜色越暗。只需执行二进制减法:
int red = 0xff0000;
int darkerRed = (0xff0000 - 0x110000);
int programmaticallyDarkerRed;
您甚至可以通过循环使其更暗:
for(int i = 1; i < 15; i++)
{
programmaticallyDarkerRed = (0xff0000 - (i * 0x110000));
}
for(int i=1;i<15;i++)
{
programmaticallydarkered=(0xff0000-(i*0x110000));
}
它越接近0x000000
或黑色,颜色就越暗。您必须给出一些上下文。数字本身没有“黑暗”属性。7不比13或0xff356483暗。您是否有一个6字节RRGGBB形式的颜色,并且您希望在保持色调/饱和度的同时降低强度?您的意思是说“给定一个RGB颜色作为十六进制数,将其转换为表示“较暗”颜色的新十六进制数”?如果是的话,“黑暗”是什么意思?如果颜色已经变暗了,比如0x002020,该怎么办?我有一个collor,比如0xAARRGGBB,其中a代表alpha,我需要它从红色->暗红色/蓝色->暗蓝色开始etc@user1462577试试我的答案。我很有信心它会成功的。只是不要减去足够多的数字使其变为负数。你必须给出一些上下文。数字本身没有“黑暗”属性。7不比13或0xff356483暗。您是否有一个6字节RRGGBB形式的颜色,并且您希望在保持色调/饱和度的同时降低强度?您的意思是说“给定一个RGB颜色作为十六进制数,将其转换为表示“较暗”颜色的新十六进制数”?如果是的话,“黑暗”是什么意思?如果颜色已经变暗了,比如0x002020,该怎么办?我有一个collor,比如0xAARRGGBB,其中a代表alpha,我需要它从红色->暗红色/蓝色->暗蓝色开始etc@user1462577试试我的答案。我很有信心它会成功的。只是不要减去足够的数字使任何数字变为负数。你可能不应该删除旧答案以发布准确的副本。如果某个答案被否决,你应该解决该答案中的问题,而不是发布新答案。对不起,不,它不起作用,它适用于某些颜色,但不适用于所有颜色,下面是它们的列表:publicstaticintpurple=0xffCC33FF;公共静态int red=0xffCC0000;公共静态int绿色=0xff33CC33;公共静态int蓝色=0xff3366FF;公共静态int黄色=0xffFFFF66;公共静态整型粉红色=0xFFFF99FF;公共静态int-white=0xffFFFFFF;公共静态int black=0xff000000;公共静态int灰色=0xff404040;公共静态int浅灰色=0xffa0a0a0;公共静态int橙色=0xffFF9900;另一方面,你应该错误地编辑你的问题,而不是删除它然后重新开始…你可能不应该删除你的旧答案以便发布一个完全相同的答案。如果某个答案被否决,你应该修复该答案中的问题,而不是发布一个新的答案。对不起,不,它不起作用,它适用于某些颜色,但不是所有颜色,下面是它们的列表:publicstaticintpurple=0xffCC33FF;公共静态int red=0xffCC0000;公共静态int绿色=0xff33CC33;公共静态int蓝色=0xff3366FF;公共静态int黄色=0xffFFFF66;公共静态整型粉红色=0xFFFF99FF;公共静态int-white=0xffFFFFFF;公共静态int black=0xff000000;公共静态int灰色=0xff404040;公共静态int浅灰色=0xffa0a0a0;公共静态int橙色=0xffFF9900;顺便说一句,你应该错误地编辑你的问题,而不是删除它然后重新开始…但是我如何将它转换回十六进制整数?@user1462577你可以使用获得argb值的int
,然后你可以使用将它转换成十六进制表示。但是我如何将它转换回十六进制整数?@user1462577你可以可以使用获取argb值的int
,然后可以使用将其转换为十六进制表示形式。但是如何将其转换回十六进制int?8位,但如何将其转换回十六进制整数?8位