首页 Android 正文
  • 本文约900字,阅读需5分钟
  • 141
  • 0

LAB颜色转RGB

摘要

public static String LABtoRGB(double inL, double inA, double inB) { double ref_x = 95.047; d...

public static String LABtoRGB(double inL, double inA, double inB) {
    double ref_x = 95.047;
    double ref_y = 100.000;
    double ref_z = 108.883;
    double x, y, z, r, g, b;
    y = (inL + 16) / 116;
    x = inA / 500 + y;
    z = y - inB / 200;
    if (y * y * y > 0.008856) {
        y = y * y * y;
    } else {
        y = (y - 16 / 116) / 7.787;
    }

    if (x * x * x > 0.008856) {
        x = x * x * x;
    } else {
        x = (x - 16 / 116) / 7.787;
    }

    if (z * z * z > 0.008856) {
        z = z * z * z;
    } else {
        z = (z - 16 / 116) / 7.787;
    }

    x = ref_x * x;
    y = ref_y * y;
    z = ref_z * z;

    x = x / 100;
    y = y / 100;
    z = z / 100;

    r = x * 3.2406 + y * (-1.5372) + z * (-0.4986);
    g = x * (-0.9689) + y * 1.8758 + z * 0.0415;
    b = x * 0.0557 + y * (-0.2040) + z * 1.0570;

    if (r > 0.0031308) {
        r = 1.055 * (Math.pow(r, (1 / 2.4))) - 0.055;
    } else {
        r = 12.92 * r;
    }
    if (g > 0.0031308) {
        g = 1.055 * (Math.pow(g, (1 / 2.4))) - 0.055;
    } else {
        g = 12.92 * g;
    }
    if (b > 0.0031308) {
        b = 1.055 * (Math.pow(b, (1 / 2.4))) - 0.055;
    } else {
        b = 12.92 * b;
    }

    r = r * 255;
    g = g * 255;
    b = b * 255;

    int toR = (int) Math.round(Math.max(Math.min(r, 255), 0));
    int toG = (int) Math.round(Math.max(Math.min(g, 255), 0));
    int toB = (int) Math.round(Math.max(Math.min(b, 255), 0));
    return toR + "," + toG + "," + toB;
}

扫描二维码,在手机上阅读
    评论