HEX到RGB转换器,Android Studio

时间:2017-05-18 20:52:56

标签: android-studio

我已经完成了将转换器RGB转换为HEX,但我没有找到执行HEX到RGB转换器的功能。对于RGB到HEX转换器,我使用了3个搜索条,我已完成项目(如代码)。

但是现在我想使用一个只有十六进制到HEX转换器的HEX值的搜索栏。但是我找不到合适的功能,我该怎么办?

enter image description here

3 个答案:

答案 0 :(得分:1)

我可以建议:

int color = Color.parseColor("#123456");

此外,您可以尝试:

public static int[] getRGB(final int hex) {
    int r = (hex & 0xFF0000) >> 16;
    int g = (hex & 0xFF00) >> 8;
    int b = (hex & 0xFF);
    return new int[] {r, g, b};
}

int hex = 0x123456; getRGB(hex);

或者,如果您需要字符串:

public static int[] getRGB(final String rgb)
{
    int r = Integer.parseInt(rgb.substring(0, 2), 16); // 16 for hex
    int g = Integer.parseInt(rgb.substring(2, 4), 16); // 16 for hex
    int b = Integer.parseInt(rgb.substring(4, 6), 16); // 16 for hex
    return new int[] {r, g, b};
}

getRGB("123456");

答案 1 :(得分:0)

在科特林:

fun getRgbFromHex(hex: String): IntArray {
            val initColor = Color.parseColor(hex)
            val r = Color.red(initColor)
            val g = Color.green(initColor)
            val b = Color.blue(initColor)
            return intArrayOf(r, g, b, )
}

答案 2 :(得分:0)

对于 ARGB,将此函数添加到您的 Android 代码中:

function sortArrayByTwoProperties(array, prop1, asc1 = true, prop2, asc2 = true) {
    return array.sort((a, b) => (
        (asc1 ? a[prop1] - b[prop1] : b[prop1] - a[prop1])
        || (asc2 ? a[prop2] - b[prop2] : b[prop2] - a[prop2])
    ));
}

array = [{ resVal: "25FA15", resFlow: 49, resName: "Rendimiento Tri-Seal Completo", resPhoto: "Tri-Sealseries.png", resHP: 1.5 },
 { resVal: "25FA2", resFlow: 52, resName: "Rendimiento Tri-Seal Completo", resPhoto: "Tri-Sealseries.png", resHP: 2 },
{ resVal: "45FA2", resFlow: 53, resName: "Rendimiento Hi-Cap Completo", resPhoto: "HighCapseries.png", resHP: 2 },
{ resVal: "35FA2", resFlow: 59, resName: "Rendimiento Hi-Cap Completo", resPhoto: "HighCapseries.png", resHP: 2 }]

// I want to sort the array by the higher "resFlow" value, but with the lowest "resHP" value.

// resHP ascending, resFlow descending
result = sortArrayByTwoProperties(array, "resHP", true, "resFlow", false);

console.log(result);

array_expected = [{ resVal: "25FA15", resFlow: 49, resName: "Rendimiento Tri-Seal Completo", resPhoto: "Tri-Sealseries.png", resHP: 1.5 },
{ resVal: "35FA2", resFlow: 59, resName: "Rendimiento Hi-Cap Completo", resPhoto: "HighCapseries.png", resHP: 2 },
{ resVal: "45FA2", resFlow: 53, resName: "Rendimiento Hi-Cap Completo", resPhoto: "HighCapseries.png", resHP: 2 },
 { resVal: "25FA2", resFlow: 52, resName: "Rendimiento Tri-Seal Completo", resPhoto: "Tri-Sealseries.png", resHP: 2 }]
 

将您的字符串颜色解析为颜色:

public static int[] getARGB(final int hex) {
    int a = (hex & 0xFF000000) >> 24;
    int r = (hex & 0xFF0000) >> 16;
    int g = (hex & 0xFF00) >> 8;
    int b = (hex & 0xFF);
    return new int[] {a, r, g, b};
}

执行:

int color = Color.parseColor("#FFFFFFFF");