Java tutorial
//package com.java2s; /* * Copyright 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { private static final double XYZ_WHITE_REFERENCE_X = 95.047; private static final double XYZ_WHITE_REFERENCE_Y = 100; private static final double XYZ_WHITE_REFERENCE_Z = 108.883; private static final double XYZ_EPSILON = 0.008856; private static final double XYZ_KAPPA = 903.3; /** * Convert RGB components to its CIE Lab representative components. * * <ul> * <li>outLab[0] is L [0 ...1)</li> * <li>outLab[1] is a [-128...127)</li> * <li>outLab[2] is b [-128...127)</li> * </ul> * * @param r red component value [0..255] * @param g green component value [0..255] * @param b blue component value [0..255] * @param outLab 3-element array which holds the resulting LAB components */ public static void RGBToLAB(int r, int g, int b, double[] outLab) { // First we convert RGB to XYZ RGBToXYZ(r, g, b, outLab); // outLab now contains XYZ XYZToLAB(outLab[0], outLab[1], outLab[2], outLab); // outLab now contains LAB representation } public static void RGBToXYZ(int r, int g, int b, double[] outXyz) { if (outXyz.length != 3) { throw new IllegalArgumentException("outXyz must have a length of 3."); } double sr = r / 255.0; sr = sr < 0.04045 ? sr / 12.92 : Math.pow((sr + 0.055) / 1.055, 2.4); double sg = g / 255.0; sg = sg < 0.04045 ? sg / 12.92 : Math.pow((sg + 0.055) / 1.055, 2.4); double sb = b / 255.0; sb = sb < 0.04045 ? sb / 12.92 : Math.pow((sb + 0.055) / 1.055, 2.4); outXyz[0] = 100 * (sr * 0.4124 + sg * 0.3576 + sb * 0.1805); outXyz[1] = 100 * (sr * 0.2126 + sg * 0.7152 + sb * 0.0722); outXyz[2] = 100 * (sr * 0.0193 + sg * 0.1192 + sb * 0.9505); } public static void XYZToLAB(double x, double y, double z, double[] outLab) { if (outLab.length != 3) { throw new IllegalArgumentException("outLab must have a length of 3."); } x = pivotXyzComponent(x / XYZ_WHITE_REFERENCE_X); y = pivotXyzComponent(y / XYZ_WHITE_REFERENCE_Y); z = pivotXyzComponent(z / XYZ_WHITE_REFERENCE_Z); outLab[0] = Math.max(0, 116 * y - 16); outLab[1] = 500 * (x - y); outLab[2] = 200 * (y - z); } private static double pivotXyzComponent(double component) { return component > XYZ_EPSILON ? Math.pow(component, 1 / 3.0) : (XYZ_KAPPA * component + 16) / 116; } }