Come posso convertire il codice esadecimale in codice RGB in Java? Principalmente su Google, i campioni sono su come convertire da RGB a esadecimale.Come convertire hex in rgb usando Java?
risposta
Credo che questo dovrebbe farlo: codice di colore
/**
*
* @param colorStr e.g. "#FFFFFF"
* @return
*/
public static Color hex2Rgb(String colorStr) {
return new Color(
Integer.valueOf(colorStr.substring(1, 3), 16),
Integer.valueOf(colorStr.substring(3, 5), 16),
Integer.valueOf(colorStr.substring(5, 7), 16));
}
Per coloro che desiderano anche una versione a 3 caratteri, si noti che nel caso di 3 caratteri ogni valore deve essere * 255/16. Ho provato questo con "000", "aaa" e "fff", e funzionano tutti correttamente adesso. – Andrew
Convertire in un numero intero, quindi divmodarlo due volte per 16, 256, 4096 o 65536 a seconda della lunghezza della stringa esadecimale originale (rispettivamente 3, 6, 9 o 12).
I codici colore esadecimali sono già rgb. Il formato è #RRGGBB
A meno che non sia #RGB, #RRRGGGBBB o #RRRRGGGGBBBB. –
public static void main(String[] args) {
int hex = 0x123456;
int r = (hex & 0xFF0000) >> 16;
int g = (hex & 0xFF00) >> 8;
int b = (hex & 0xFF);
}
Un esagono è #RRGGBB
RR, GG, BB sono valori esadecimali che vanno 0-255
chiamata RR XY Let dove X e Y sono hex carattere 0-9A-F, A = 10, M = 15
Il valore decimale è X * 1 6 + Y
Se RR = B7, il decimale per B è 11, quindi il valore è 11 * 16 + 7 = 183
public int[] getRGB(String rgb){
int[] ret = new int[3];
for(int i=0; i<3; i++){
ret[i] = hexToInt(rgb.charAt(i*2), rgb.charAt(i*2+1));
}
return ret;
}
public int hexToInt(char a, char b){
int x = a < 65 ? a-48 : a-55;
int y = b < 65 ? b-48 : b-55;
return x*16+y;
}
Per Android sviluppo, io uso:
int color = Color.parseColor("#123456");
Basta sostituire il '#' con '0x' –
Per approfondire ° e risposta @xhh fornita, è possibile aggiungere il rosso, il verde e il blu per formattare la stringa come "rgb (0,0,0)" prima di restituirla.
/**
*
* @param colorStr e.g. "#FFFFFF"
* @return String - formatted "rgb(0,0,0)"
*/
public static String hex2Rgb(String colorStr) {
Color c = new Color(
Integer.valueOf(hexString.substring(1, 3), 16),
Integer.valueOf(hexString.substring(3, 5), 16),
Integer.valueOf(hexString.substring(5, 7), 16));
StringBuffer sb = new StringBuffer();
sb.append("rgb(");
sb.append(c.getRed());
sb.append(",");
sb.append(c.getGreen());
sb.append(",");
sb.append(c.getBlue());
sb.append(")");
return sb.toString();
}
si può fare semplicemente come di seguito:
public static int[] getRGB(final String rgb)
{
final int[] ret = new int[3];
for (int i = 0; i < 3; i++)
{
ret[i] = Integer.parseInt(rgb.substring(i * 2, i * 2 + 2), 16);
}
return ret;
}
Per esempio
getRGB("444444") = 68,68,68
getRGB("FFFFFF") = 255,255,255
Un sacco di queste soluzioni funzionano, ma questo è un'alternativa.
String hex="#00FF00"; // green
long thisCol=Long.decode(hex)+4278190080L;
int useColour=(int)thisCol;
Se non aggiungere 4.278.190,08 mila (# FF000000) il colore ha un Alpha di 0 e non mostrerà.
L'altro giorno ero stato risolvere il problema simile e ho trovato conveniente per convertire la stringa esadecimale del colore in int array [alpha, r, g, b]:
/**
* Hex color string to int[] array converter
*
* @param hexARGB should be color hex string: #AARRGGBB or #RRGGBB
* @return int[] array: [alpha, r, g, b]
* @throws IllegalArgumentException
*/
public static int[] hexStringToARGB(String hexARGB) throws IllegalArgumentException {
if (!hexARGB.startsWith("#") || !(hexARGB.length() == 7 || hexARGB.length() == 9)) {
throw new IllegalArgumentException("Hex color string is incorrect!");
}
int[] intARGB = new int[4];
if (hexARGB.length() == 9) {
intARGB[0] = Integer.valueOf(hexARGB.substring(1, 3), 16); // alpha
intARGB[1] = Integer.valueOf(hexARGB.substring(3, 5), 16); // red
intARGB[2] = Integer.valueOf(hexARGB.substring(5, 7), 16); // green
intARGB[3] = Integer.valueOf(hexARGB.substring(7), 16); // blue
} else hexStringToARGB("#FF" + hexARGB.substring(1));
return intARGB;
}
Ecco una versione che gestisce sia RGB e RGBA versioni:
/**
* Converts a hex string to a color. If it can't be converted null is returned.
* @param hex (i.e. #CCCCCCFF or CCCCCC)
* @return Color
*/
public static Color HexToColor(String hex)
{
hex = hex.replace("#", "");
switch (hex.length()) {
case 6:
return new Color(
Integer.valueOf(hex.substring(0, 2), 16),
Integer.valueOf(hex.substring(2, 4), 16),
Integer.valueOf(hex.substring(4, 6), 16));
case 8:
return new Color(
Integer.valueOf(hex.substring(0, 2), 16),
Integer.valueOf(hex.substring(2, 4), 16),
Integer.valueOf(hex.substring(4, 6), 16),
Integer.valueOf(hex.substring(6, 8), 16));
}
return null;
}
Se non si desidera utilizzare l'AWT Color.decode, poi basta copiare il contenuto del metodo:
int i = Integer.decode("#FFFFFF");
int[] rgb = new int[]{(i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF};
Numero intero.decodifica gestisce il # o 0x, a seconda di come è formattata la stringa
- 1. come convertire hex in base64
- 2. Come convertire String in Hex e Hex in String?
- 3. Come convertire il valore BigInteger in Hex in Java
- 4. Chrome DevTools converte tutti i colori HEX in RGB
- 5. come convertire il colore rgb in int in java
- 6. CSS - RGB o HEX per il colore
- 7. Java: come convertire il colore RGB in CIE Lab
- 8. Converti da HEX a struct RGB in C
- 9. Come si converte hex in decimale usando VB.NET?
- 10. Come convertire "background-color" in formato rgb()?
- 11. come convertire un'immagine RGB in array numpy?
- 12. Arduino: Convertire un hex Sting "#FFFFFF" in 3 int
- 13. Come convertire .wav in .mp3 usando Java?
- 14. Come posso convertire da int a hex in Golang?
- 15. Come convertire il colore HEX in rgba con Less compilatore?
- 16. Ricerca di colori HEX simili usando una soglia
- 17. Conversione da int RGB in esadecimale
- 18. Incrementa un valore Hex (JAVA)
- 19. Java: convertire java.awt.Color in javafx.scene.paint.Color
- 20. Come convertire l'array di byte in formato esadecimale in Java
- 21. efficiente convertire tra Hex, binario e decimale in C/C++
- 22. Convertire un'immagine in un array RGB in Javascript
- 23. convertire RGB in scala di grigi in C
- 24. Valore HEX in Objective-C
- 25. Algoritmo per convertire qualsiasi intero positivo in un valore RGB
- 26. Leggi hex in C# utilizzando IO
- 27. C++ Hex Parsing
- 28. Come convertire IPv4 in intero usando CoffeScript?
- 29. Come convertire HTML in testo usando jQuery?
- 30. Come posso ottenere una stringa esadecimale da UIColor o da rgb
Puoi dare un esempio di cosa stai provando a convertire e in cosa stai cercando di convertire? Non è chiaro esattamente cosa stai cercando di fare. – kkress
000000 verrà convertito in colore nero rgb – user236501