Converts an XYZ array to an Lab array
Algorithm from http://www.brucelindbloom.com/Eqn_XYZ_to_Lab.html
Reference whites from http://brucelindbloom.com/Eqn_ChromAdapt.html#CommonChromAdapt
348993
public static function xyz2lab($xyz)
    {
        $x = $xyz[0] / 0.95047;
        $y = $xyz[1] / 1.00000;
        $z = $xyz[2] / 1.08883;

        $f = function ($n) {
            if ($n > 0.008856) {
                return pow($n, 1/3);
            }
            return (903.3 * $n + 16) / 116;
        };
        $fx = $f($x);
        $fy = $f($y);
        $fz = $f($z);
        return array(
            116 * $fy - 16,
            500 * ($fx - $fy),
            200 * ($fy - $fz)
        );
    }
53024
def Lab_to_LCHab(cobj, *args, **kwargs):
    
    lch_l = cobj.lab_l
    lch_c = math.sqrt(
        math.pow(float(cobj.lab_a), 2) + math.pow(float(cobj.lab_b), 2))
    lch_h = math.atan2(float(cobj.lab_b), float(cobj.lab_a))

    if lch_h > 0:
        lch_h = (lch_h / math.pi) * 180
    else:
        lch_h = 360 - (math.fabs(lch_h) / math.pi) * 180

    return LCHabColor(
        lch_l, lch_c, lch_h, observer=cobj.observer, illuminant=cobj.illuminant)
839824
function Converter (rgbSpace=workspace.sRGB, whitePoint=illuminant.D65) {
	var primaries = [rgbSpace.r, rgbSpace.g, rgbSpace.b];

	var M_P = matrix.transpose(primaries.map(function (v) {
		var X = v.x / v.y,
		    Y = 1,
		    Z = (1 - v.x - v.y) / v.y;
		return [X, Y, Z];
	}));

	var gamma = rgbSpace.gamma,
	    M_S = matrix.multiply(matrix.inverse(M_P), whitePoint),
	    M_RGB_XYZ = matrix.scalar(M_P, M_S),
	    M_XYZ_RGB = matrix.inverse(M_RGB_XYZ);

	return {
		fromRgb (RGB) {
			return matrix.multiply(M_RGB_XYZ, RGB.map(gamma.decode));
		},
		toRgb (XYZ) {
			return matrix.multiply(M_XYZ_RGB, XYZ).map(gamma.encode);
		},
	};
}
331760
function getXYZ(color) {
    var r = toXYZValue(color[0]);
    var g = toXYZValue(color[1]);
    var b = toXYZValue(color[2]);

    return [
        r * 0.4124 + g * 0.3576 + b * 0.1805,
        r * 0.2126 + g * 0.7152 + b * 0.0722,
        r * 0.0193 + g * 0.1192 + b * 0.9505
    ]
}
684763
function Color(x, y, z, m) {
      var me, _ref2;
      me = this;
      if (!(x != null) && !(y != null) && !(z != null) && !(m != null)) {
        x = [255, 0, 255];
      }
      if (type(x) === "array" && x.length === 3) {
        if (m == null) m = y;
        _ref2 = x, x = _ref2[0], y = _ref2[1], z = _ref2[2];
      }
      if (type(x) === "string") {
        m = 'hex';
      } else {
        if (m == null) m = 'rgb';
      }
      if (m === 'rgb') {
        me.rgb = [x, y, z];
      } else if (m === 'hsl') {
        me.rgb = Color.hsl2rgb(x, y, z);
      } else if (m === 'hsv') {
        me.rgb = Color.hsv2rgb(x, y, z);
      } else if (m === 'hex') {
        me.rgb = Color.hex2rgb(x);
      } else if (m === 'lab') {
        me.rgb = Color.lab2rgb(x, y, z);
      } else if (m === 'csl') {
        me.rgb = Color.csl2rgb(x, y, z);
      } else if (m === 'hsi') {
        me.rgb = Color.hsi2rgb(x, y, z);
      }
    }
1125771
def from_array(x, frame='unspecified'):
        

        if not Image.can_convert(x):
            raise ValueError('Cannot convert array to an Image!')

        dtype = x.dtype
        height = x.shape[0]
        width = x.shape[1]
        channels = 1
        if len(x.shape) == 3:
            channels = x.shape[2]
        if dtype == np.uint8:
            if channels == 1:
                if np.any((x % BINARY_IM_MAX_VAL) > 0):
                    return GrayscaleImage(x, frame)
                return BinaryImage(x, frame)
            elif channels == 3:
                return ColorImage(x, frame)
            else:
                raise ValueError(
                    'No available image conversion for uint8 array with 2 channels')
        elif dtype == np.uint16:
            if channels != 1:
                raise ValueError(
                    'No available image conversion for uint16 array with 2 or 3 channels')
            return GrayscaleImage(x, frame)
        elif dtype == np.float32 or dtype == np.float64:
            if channels == 1:
                return DepthImage(x, frame)
            elif channels == 2:
                return GdImage(x, frame)
            elif channels == 3:
                logging.warning('Converting float array to uint8')
                return ColorImage(x.astype(np.uint8), frame)
            return RgbdImage(x, frame)
        else:
            raise ValueError(
                'Conversion for dtype %s not supported!' %
                (str(dtype)))
798227
def _to_array(self, alpha, normalization='4pi', csphase=1):
        
        coeffs = _shtools.SHVectorToCilm(self.tapers[:, alpha])

        if normalization == 'schmidt':
            for l in range(self.lmax + 1):
                coeffs[:, l, :l+1] *= _np.sqrt(2.0 * l + 1.0)
        elif normalization == 'ortho':
            coeffs *= _np.sqrt(4.0 * _np.pi)

        if csphase == -1:
            for m in range(self.lmax + 1):
                if m % 2 == 1:
                    coeffs[:, :, m] = - coeffs[:, :, m]

        return coeffs
229170
def mpl_color2xcolor(data, matplotlib_color):
    
    # Convert it to RGBA.
    my_col = numpy.array(mpl.colors.ColorConverter().to_rgba(matplotlib_color))

    # If the alpha channel is exactly 0, then the color is really 'none'
    # regardless of the RGB channels.
    if my_col[-1] == 0.0:
        return data, "none", my_col

    xcol = None
    # RGB values (as taken from xcolor.dtx):
    available_colors = {
        # List white first such that for gray values, the combination
        # white!<x>!black is preferred over, e.g., gray!<y>!black. Note that
        # the order of the dictionary is respected from Python 3.6 on.
        "white": numpy.array([1, 1, 1]),
        "lightgray": numpy.array([0.75, 0.75, 0.75]),
        "gray": numpy.array([0.5, 0.5, 0.5]),
        "darkgray": numpy.array([0.25, 0.25, 0.25]),
        "black": numpy.array([0, 0, 0]),
        #
        "red": numpy.array([1, 0, 0]),
        "green": numpy.array([0, 1, 0]),
        "blue": numpy.array([0, 0, 1]),
        "brown": numpy.array([0.75, 0.5, 0.25]),
        "lime": numpy.array([0.75, 1, 0]),
        "orange": numpy.array([1, 0.5, 0]),
        "pink": numpy.array([1, 0.75, 0.75]),
        "purple": numpy.array([0.75, 0, 0.25]),
        "teal": numpy.array([0, 0.5, 0.5]),
        "violet": numpy.array([0.5, 0, 0.5]),
        # The colors cyan, magenta, yellow, and olive are also
        # predefined by xcolor, but their RGB approximation of the
        # native CMYK values is not very good. Don't use them here.
    }

    available_colors.update(data["custom colors"])

    # Check if it exactly matches any of the colors already available.
    # This case is actually treated below (alpha==1), but that loop
    # may pick up combinations with black before finding the exact
    # match. Hence, first check all colors.
    for name, rgb in available_colors.items():
        if all(my_col[:3] == rgb):
            xcol = name
            return data, xcol, my_col

    # Check if my_col is a multiple of a predefined color and 'black'.
    for name, rgb in available_colors.items():
        if name == "black":
            continue

        if rgb[0] != 0.0:
            alpha = my_col[0] / rgb[0]
        elif rgb[1] != 0.0:
            alpha = my_col[1] / rgb[1]
        else:
            assert rgb[2] != 0.0
            alpha = my_col[2] / rgb[2]

        # The cases 0.0 (my_col == black) and 1.0 (my_col == rgb) are
        # already accounted for by checking in available_colors above.
        if all(my_col[:3] == alpha * rgb) and 0.0 < alpha < 1.0:
            xcol = name + ("!{}!black".format(alpha * 100))
            return data, xcol, my_col

    # Lookup failed, add it to the custom list.
    xcol = "color" + str(len(data["custom colors"]))
    data["custom colors"][xcol] = my_col[:3]

    return data, xcol, my_col
225334
function() {
        var tmp = [], //12
			src = [], //16
			dst = [], //16
			mat = this.toArray(),
			i = 0;

        for (i = 0; i < 4; i++) {
            var i4 = i << 2;
            src[i] = mat[i4];
            src[i + 4] = mat[i4 + 1];
            src[i + 8] = mat[i4 + 2];
            src[i + 12] = mat[i4 + 3];
        }

        // calculate pairs for first 8 elements (cofactors)
        tmp[0] = src[10] * src[15];
        tmp[1] = src[11] * src[14];
        tmp[2] = src[9] * src[15];
        tmp[3] = src[11] * src[13];
        tmp[4] = src[9] * src[14];
        tmp[5] = src[10] * src[13];
        tmp[6] = src[8] * src[15];
        tmp[7] = src[11] * src[12];
        tmp[8] = src[8] * src[14];
        tmp[9] = src[10] * src[12];
        tmp[10] = src[8] * src[13];
        tmp[11] = src[9] * src[12];

        // calculate first 8 elements (cofactors)
        var src0 = src[0],
			src1 = src[1],
			src2 = src[2],
			src3 = src[3],
			src4 = src[4],
			src5 = src[5],
			src6 = src[6],
			src7 = src[7];
		dst[0] = tmp[0] * src5 + tmp[3] * src6 + tmp[4] * src7;
		dst[0] -= tmp[1] * src5 + tmp[2] * src6 + tmp[5] * src7;
		dst[1] = tmp[1] * src4 + tmp[6] * src6 + tmp[9] * src7;
		dst[1] -= tmp[0] * src4 + tmp[7] * src6 + tmp[8] * src7;
		dst[2] = tmp[2] * src4 + tmp[7] * src5 + tmp[10] * src7;
		dst[2] -= tmp[3] * src4 + tmp[6] * src5 + tmp[11] * src7;
		dst[3] = tmp[5] * src4 + tmp[8] * src5 + tmp[11] * src6;
		dst[3] -= tmp[4] * src4 + tmp[9] * src5 + tmp[10] * src6;
		dst[4] = tmp[1] * src1 + tmp[2] * src2 + tmp[5] * src3;
		dst[4] -= tmp[0] * src1 + tmp[3] * src2 + tmp[4] * src3;
		dst[5] = tmp[0] * src0 + tmp[7] * src2 + tmp[8] * src3;
		dst[5] -= tmp[1] * src0 + tmp[6] * src2 + tmp[9] * src3;
		dst[6] = tmp[3] * src0 + tmp[6] * src1 + tmp[11] * src3;
		dst[6] -= tmp[2] * src0 + tmp[7] * src1 + tmp[10] * src3;
		dst[7] = tmp[4] * src0 + tmp[9] * src1 + tmp[10] * src2;
		dst[7] -= tmp[5] * src0 + tmp[8] * src1 + tmp[11] * src2;

        // calculate pairs for second 8 elements (cofactors)
		tmp[0] = src2 * src7;
		tmp[1] = src3 * src6;
		tmp[2] = src1 * src7;
		tmp[3] = src3 * src5;
		tmp[4] = src1 * src6;
		tmp[5] = src2 * src5;
		tmp[6] = src0 * src7;
		tmp[7] = src3 * src4;
		tmp[8] = src0 * src6;
		tmp[9] = src2 * src4;
		tmp[10] = src0 * src5;
		tmp[11] = src1 * src4;

        // calculate second 8 elements (cofactors)
		src0 = src[8];
		src1 = src[9];
		src2 = src[10];
		src3 = src[11];
		src4 = src[12];
		src5 = src[13];
		src6 = src[14];
		src7 = src[15];
		dst[8] = tmp[0] * src5 + tmp[3] * src6 + tmp[4] * src7;
		dst[8] -= tmp[1] * src5 + tmp[2] * src6 + tmp[5] * src7;
		dst[9] = tmp[1] * src4 + tmp[6] * src6 + tmp[9] * src7;
		dst[9] -= tmp[0] * src4 + tmp[7] * src6 + tmp[8] * src7;
		dst[10] = tmp[2] * src4 + tmp[7] * src5 + tmp[10] * src7;
		dst[10] -= tmp[3] * src4 + tmp[6] * src5 + tmp[11] * src7;
		dst[11] = tmp[5] * src4 + tmp[8] * src5 + tmp[11] * src6;
		dst[11] -= tmp[4] * src4 + tmp[9] * src5 + tmp[10] * src6;
		dst[12] = tmp[2] * src2 + tmp[5] * src3 + tmp[1] * src1;
		dst[12] -= tmp[4] * src3 + tmp[0] * src1 + tmp[3] * src2;
		dst[13] = tmp[8] * src3 + tmp[0] * src0 + tmp[7] * src2;
		dst[13] -= tmp[6] * src2 + tmp[9] * src3 + tmp[1] * src0;
		dst[14] = tmp[6] * src1 + tmp[11] * src3 + tmp[3] * src0;
		dst[14] -= tmp[10] * src3 + tmp[2] * src0 + tmp[7] * src1;
		dst[15] = tmp[10] * src2 + tmp[4] * src0 + tmp[9] * src1;
		dst[15] -= tmp[8] * src1 + tmp[11] * src2 + tmp[5] * src0;

		var det = 1.0 / (src[0] * dst[0] + src[1] * dst[1] + src[2] * dst[2] + src[3] * dst[3]);
		for (i = 0, k = 0; i < 4; i++) {
			var m = this.matrix[i];
			for (var j = 0; j < 4; j++) {
				m[j] = dst[k++] * det;
			}
		}
		return this;
    }
1064129
function convertToRgb(cs, src, srcOffset, maxVal, dest, destOffset) {
    // XXX: Lab input is in the range of [0, 100], [amin, amax], [bmin, bmax]
    // not the usual [0, 1]. If a command like setFillColor is used the src
    // values will already be within the correct range. However, if we are
    // converting an image we have to map the values to the correct range given
    // above.
    // Ls,as,bs <---> L*,a*,b* in the spec
    var Ls = src[srcOffset];
    var as = src[srcOffset + 1];
    var bs = src[srcOffset + 2];
    if (maxVal !== false) {
      Ls = decode(Ls, maxVal, 0, 100);
      as = decode(as, maxVal, cs.amin, cs.amax);
      bs = decode(bs, maxVal, cs.bmin, cs.bmax);
    }

    // Adjust limits of 'as' and 'bs'
    as = as > cs.amax ? cs.amax : as < cs.amin ? cs.amin : as;
    bs = bs > cs.bmax ? cs.bmax : bs < cs.bmin ? cs.bmin : bs;

    // Computes intermediate variables X,Y,Z as per spec
    var M = (Ls + 16) / 116;
    var L = M + (as / 500);
    var N = M - (bs / 200);

    var X = cs.XW * fn_g(L);
    var Y = cs.YW * fn_g(M);
    var Z = cs.ZW * fn_g(N);

    var r, g, b;
    // Using different conversions for D50 and D65 white points,
    // per http://www.color.org/srgb.pdf
    if (cs.ZW < 1) {
      // Assuming D50 (X=0.9642, Y=1.00, Z=0.8249)
      r = X * 3.1339 + Y * -1.6170 + Z * -0.4906;
      g = X * -0.9785 + Y * 1.9160 + Z * 0.0333;
      b = X * 0.0720 + Y * -0.2290 + Z * 1.4057;
    } else {
      // Assuming D65 (X=0.9505, Y=1.00, Z=1.0888)
      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;
    }
    // clamp color values to [0,1] range then convert to [0,255] range.
    dest[destOffset] = r <= 0 ? 0 : r >= 1 ? 255 : Math.sqrt(r) * 255 | 0;
    dest[destOffset + 1] = g <= 0 ? 0 : g >= 1 ? 255 : Math.sqrt(g) * 255 | 0;
    dest[destOffset + 2] = b <= 0 ? 0 : b >= 1 ? 255 : Math.sqrt(b) * 255 | 0;
  }
7591
def _convert_bool_array(self, array):
        

        output = (array.view(numpy.int8) == ord('T')).astype(numpy.bool)
        return output
1028839
function rgbaToColorMatrix(red, green, blue) {
  var alpha = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;

  var decToFloat = function decToFloat(value) {
    return Math.round(value / 255 * 10) / 10;
  };
  var redFloat = decToFloat(red);
  var greenFloat = decToFloat(green);
  var blueFloat = decToFloat(blue);
  var alphaFloat = decToFloat(alpha);
  return '0 0 0 0 ' + redFloat + ' 0 0 0 0 ' + greenFloat + ' 0 0 0 0 ' + blueFloat + ' 0 0 0 1 ' + alphaFloat;
}
461375
def colorize(arr, colors, values):
    
    hcolors = np.array([rgb2hcl(*i[:3]) for i in colors])
    # unwrap colormap in hcl space
    hcolors[:, 0] = np.rad2deg(np.unwrap(np.deg2rad(np.array(hcolors)[:, 0])))
    channels = [np.interp(arr,
                          np.array(values),
                          np.array(hcolors)[:, i])
                for i in range(3)]

    channels = list(hcl2rgb(*channels))

    rest = [np.interp(arr,
                      np.array(values),
                      np.array(colors)[:, i + 3])
            for i in range(np.array(colors).shape[1] - 3)]

    channels.extend(rest)

    try:
        return [np.ma.array(channel, mask=arr.mask) for channel in channels]
    except AttributeError:
        return channels
729860
def to_array(color_model = :rgb)
      color_model = color_model.to_sym unless color_model.is_a? Symbol
      if [:rgb, :hsl].include?(color_model)
        array = colors.map { |c| c.to_array(color_model) }
      elsif color_model == :hex
        array = colors.map{ |c| c.hex }
      else
        raise(ArgumentError, "Argument must be :rgb, :hsl, or :hex")
      end
      array
    end
957957
static double hsla( double h, double s, double l, double a ) {

        h = (h % 360) / 360;
        s = clamp(s); l = clamp(l); a = clamp(a);

        double m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
        double m1 = l * 2 - m2;

        return rgba( hslaHue(h + 1.0/3, m1, m2) * 255,
                     hslaHue(h        , m1, m2) * 255,
                     hslaHue(h - 1.0/3, m1, m2) * 255,
                     a);
    }
734643
def to_xyz100(self, data, description):
        
        rgb_c = compute_to(data, description, self)
        # Step 6: Calculate R, G and B
        # rgb = (rgb_c.T / self.D_RGB).T
        # Step 7: Calculate X, Y and Z
        # xyz = self.solve_M16(rgb)
        return dot(self.invM_, rgb_c)
788985
def rgb2lab(color)
      r, g, b = color.r * 255, color.g * 255, color.b * 255
      r = rgb_xyz r
      g = rgb_xyz g
      b = rgb_xyz b
      x = xyz_lab (0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / X
      y = xyz_lab (0.2126729 * r + 0.7151522 * g + 0.0721750 * b) / Y
      z = xyz_lab (0.0193339 * r + 0.1191920 * g + 0.9503041 * b) / Z
      [116 * y - 16, 500 * (x - y), 200 * (y - z)]
    end