Java'da hex rengi RGB koduna nasıl dönüştürebilirim? Çoğunlukla Google'da örnekler RGB'den hex'e nasıl dönüştürüleceği üzerinedir.
Java'da hex rengi RGB koduna nasıl dönüştürebilirim? Çoğunlukla Google'da örnekler RGB'den hex'e nasıl dönüştürüleceği üzerinedir.
Yanıtlar:
Sanırım bunu yapmalı:
/**
*
* @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 ) );
}
Aslında bunu yapmanın daha kolay (yerleşik) bir yolu var:
Color.decode("#FFCCEE");
public static void main(String[] args) {
int hex = 0x123456;
int r = (hex & 0xFF0000) >> 16;
int g = (hex & 0xFF00) >> 8;
int b = (hex & 0xFF);
}
İçin Android geliştirme, ben kullanın:
int color = Color.parseColor("#123456");
Hem RGB hem de RGBA sürümlerini işleyen bir sürüm:
/**
* 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;
}
Onaltılık renk kodu: #RRGGBB
RR, GG, BB, 0-255 arasında değişen hex değerlerdir
RR XY diyelim burada X ve Y onaltılık karakter 0-9A-F, A = 10, F = 15
Ondalık değer X * 16 + Y'dir.
RR = B7 ise, B için ondalık 11'dir, yani değer 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;
}
bunu aşağıdaki gibi basitçe yapabilirsiniz:
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;
}
Örneğin
getRGB("444444") = 68,68,68
getRGB("FFFFFF") = 255,255,255
Bunu bir tam sayıya dönüştürün, ardından orijinal onaltılık dizenin uzunluğuna bağlı olarak (sırasıyla 3, 6, 9 veya 12) 16, 256, 4096 veya 65536 ile iki kez bölün.
@Xhh verilen yanıtı ayrıntılarıyla açıklamak için, döndürmeden önce dizenizi "rgb (0,0,0)" olarak biçimlendirmek için kırmızı, yeşil ve maviyi ekleyebilirsiniz.
/**
*
* @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();
}
İşte RGBA sürümlerini işleyen daha hızlı bir sürüm:
public static int hexToIntColor(String hex){
int Alpha = Integer.valueOf(hex.substring(0, 2), 16);
int Red = Integer.valueOf(hex.substring(2, 4), 16);
int Green = Integer.valueOf(hex.substring(4, 6), 16);
int Blue = Integer.valueOf(hex.substring(6, 8), 16);
Alpha = (Alpha << 24) & 0xFF000000;
Red = (Red << 16) & 0x00FF0000;
Green = (Green << 8) & 0x0000FF00;
Blue = Blue & 0x000000FF;
return Alpha | Red | Green | Blue;
}
En kolay yol:
// 0000FF
public static Color hex2Rgb(String colorStr) {
return new Color(Integer.valueOf(colorStr, 16));
}
Android Kotlin kullanıcıları için:
"#FFF".longARGB()?.let{ Color.parceColor(it) }
"#FFFF".longARGB()?.let{ Color.parceColor(it) }
fun String?.longARGB(): String? {
if (this == null || !startsWith("#")) return null
// #RRGGBB or #AARRGGBB
if (length == 7 || length == 9) return this
// #RGB or #ARGB
if (length in 4..5) {
val rgb = "#${this[1]}${this[1]}${this[2]}${this[2]}${this[3]}${this[3]}"
if (length == 5) {
return "$rgb${this[4]}${this[4]}"
}
return rgb
}
return null
}
Onaltılık renk kodları zaten rgb'dir. Biçim şu şekildedir: #RRGGBB
Geçen gün benzer sorunu çözüyordum ve onaltılık renkli dizeyi int dizi [alfa, r, g, b] 'ye dönüştürmeyi uygun buldum:
/**
* 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;
}
For shortened hex code like #fff or #000
int red = "colorString".charAt(1) == '0' ? 0 :
"colorString".charAt(1) == 'f' ? 255 : 228;
int green =
"colorString".charAt(2) == '0' ? 0 : "colorString".charAt(2) == 'f' ?
255 : 228;
int blue = "colorString".charAt(3) == '0' ? 0 :
"colorString".charAt(3) == 'f' ? 255 : 228;
Color.rgb(red, green,blue);