简体   繁体   English

我如何在 Java 中将 RGB 颜色转换为十六进制

[英]How do i convert RGB color to hexadecimal in Java

I've searched everywhere, but i couldn't find a suiting solution.我到处搜索,但找不到合适的解决方案。

I have a RGB color that i want to convert to this format: 0xffffffff我有一个 RGB 颜色要转换为这种格式: 0xffffffff

Many answers convert it to this format: #ffffff许多答案将其转换为这种格式: #ffffff

Does anyone know how to convert it to this format ( 0xffffffff )?有谁知道如何将其转换为这种格式( 0xffffffff )?

EDIT: Solved!编辑:解决了! I actually needed a int but i accidentally said string, woopsie.我实际上需要一个 int 但我不小心说了字符串,woopsie。 Seems to work now, thanks!现在似乎可以工作了,谢谢!

Snippet:片段:

double frequency = .3;
double r = 255;
double g = 255;
double b = 255;
for (int i = 0; i < 32; ++i)
{
    // I am trying to make a rainbow, hence this for loop and the Math.sin
    r = Math.sin(frequency*i + 0) * 127 + 128;
    g = Math.sin(frequency*i + 2) * 127 + 128;
    b = Math.sin(frequency*i + 4) * 127 + 128;
    int red = (int)r;
    int green = (int)g;
    int blue = (int)b;

    // converting, outputs a string

    System.out.println(result);
}

In case you want the number not as an string , but as an int :如果您希望数字不是string ,而是int


0xRRGGBB

RR , GG , BB each represent a value between 0 - 255 (the RGB-values). RRGGBB各代表一个介于 0 - 255(RGB 值)之间的值。 Each digit of the hex-number can have values ranging from 0 - 16, means that you will need to store numbers up to 16 ^ 6 = 16.777.216‬ -> dataype int should be ok.十六进制数的每个数字的值范围为 0 - 16,这意味着您需要存储最多16 ^ 6 = 16.777.216‬ -> dataype int应该没问题。
Then simply make your int :然后简单地制作你的int

int hexval = red * 65536 + green * 256 + blue;


Optionally you can output it: 您可以选择输出它:

 System.out.printf("0x%06X\\n", hexval);

I've searched everywhere, but i couldn't find a suiting solution.我到处搜索过,但是找不到合适的解决方案。

I have a RGB color that i want to convert to this format: 0xffffffff我有一个要转换为以下格式的RGB颜色: 0xffffffff

Many answers convert it to this format: #ffffff许多答案将其转换为以下格式: #ffffff

Does anyone know how to convert it to this format ( 0xffffffff )?有谁知道如何将其转换为这种格式( 0xffffffff )?

EDIT: Solved!编辑:解决了! I actually needed a int but i accidentally said string, woopsie.我实际上需要一个int,但是我不小心说了字符串,woopsie。 Seems to work now, thanks!看来现在可以工作了,谢谢!

Snippet:片段:

double frequency = .3;
double r = 255;
double g = 255;
double b = 255;
for (int i = 0; i < 32; ++i)
{
    // I am trying to make a rainbow, hence this for loop and the Math.sin
    r = Math.sin(frequency*i + 0) * 127 + 128;
    g = Math.sin(frequency*i + 2) * 127 + 128;
    b = Math.sin(frequency*i + 4) * 127 + 128;
    int red = (int)r;
    int green = (int)g;
    int blue = (int)b;

    // converting, outputs a string

    System.out.println(result);
}
public static int colorToInt(int r, int g, int b) {
    int result = 0;
    result += r;
    result = result << 8;
    result += g;
    result = result << 8;
    result += b;

    return result;
}

Here's an example:下面是一个例子:

System.out.println(0xffffff); // 16777215
System.out.println(colorToInt(255, 255, 255)); // 16777215

Repl: https://repl.it/repls/IrritatingTwinConsulting .回复: https : //repl.it/repls/IrritatingTwinConsulting

This function accepts int, instead of double, but I don't think it's a problem.这个函数接受int,而不是double,但我认为这不是问题。 Hope I understood your question correctly.希望我正确理解了你的问题。

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

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