LoginSignup
21
13

More than 5 years have passed since last update.

JavaScriptでRGBからLab色空間への変換

Last updated at Posted at 2017-02-08

RGBからLab色空間に変換するJavaScriptスクリプトです。変換のアルゴリズムは以下のwikipediaのページを参考にしています。

RGBからLabに変換するためのアルゴリズムを探してみたのですが、見つけることができなかったため、RGBからXYZに変換して、XYZからLabに変換といった事を行なっています。

rgbToLab
function rgbToLab(r,g,b) {
    //https://en.wikipedia.org/wiki/SRGB#The_reverse_transformation
    var r = r / 255;
    var g = g / 255;
    var b = b / 255;

    r = r > 0.04045 ? Math.pow(((r + 0.055) / 1.055), 2.4) : (r / 12.92);
    g = g > 0.04045 ? Math.pow(((g + 0.055) / 1.055), 2.4) : (g / 12.92);
    b = b > 0.04045 ? Math.pow(((b + 0.055) / 1.055), 2.4) : (b / 12.92);

    var x = (r * 0.4124) + (g * 0.3576) + (b * 0.1805);
    var y = (r * 0.2126) + (g * 0.7152) + (b * 0.0722);
    var z = (r * 0.0193) + (g * 0.1192) + (b * 0.9505);

    //https://en.wikipedia.org/wiki/Lab_color_space#CIELAB-CIEXYZ_conversions
    var L;
    var a;
    var b;

    x *= 100;
    y *= 100;
    z *= 100;

    x /= 95.047;
    y /= 100;
    z /= 108.883;

    x = x > 0.008856 ? Math.pow(x, 1 / 3) : (7.787 * x) + (4 / 29);
    y = y > 0.008856 ? Math.pow(y, 1 / 3) : (7.787 * y) + (4 / 29);
    z = z > 0.008856 ? Math.pow(z, 1 / 3) : (7.787 * z) + (4 / 29);

    L = (116 * y) - 16;
    a = 500 * (x - y);
    b = 200 * (y - z);

    return [L, a, b];
}

関連リンク

21
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
13