Lucaskyy:
He buscado por todas partes, pero no pude encontrar una solución satisfaciendo.
Tengo un color RGB que quiero convertir a este formato: 0xffffffff
Muchas de las respuestas convierten a este formato: #ffffff
¿Alguien sabe cómo convertir a este formato ( 0xffffffff
)?
EDIT: Resuelto! realmente necesitaba un int pero accidentalmente dicha cadena, woopsie. Parece que funciona ahora, gracias!
Retazo:
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);
}
Timoteo Sosnowski:
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;
}
He aquí un ejemplo:
System.out.println(0xffffff); // 16777215
System.out.println(colorToInt(255, 255, 255)); // 16777215
REPL: https://repl.it/repls/IrritatingTwinConsulting .
Esta función acepta int, en lugar de doble, pero no creo que sea un problema. Esperanza entendí bien su pregunta.