Convert physically linear RGB values into sRGB ones. The transform is
    uniform in the components, so *linsrgb* can be of any shape.

    *linsrgb* values should range between 0 and 1, inclusively.
426188
def linsrgb_to_srgb (linsrgb):
    
    # From Wikipedia, but easy analogue to the above.
    gamma = 1.055 * linsrgb**(1./2.4) - 0.055
    scale = linsrgb * 12.92
    return np.where (linsrgb > 0.0031308, gamma, scale)
950604
def rgb2rgba(rgb):
    """"""
    rgba = []
    for i in range(0, len(rgb), 3):
        rgba += rgb[i:i+3]
        rgba.append(255)

    return rgba
1176561
public static void hsvToRgb( double h , double s , double v , double []rgb ) {
		if( s == 0 ) {
			rgb[0] = v;
			rgb[1] = v;
			rgb[2] = v;
			return;
		}
		h /= d60_F64;
		int h_int = (int)h;
		double remainder = h - h_int;
		double p = v * ( 1 - s );
		double q = v * ( 1 - s * remainder );
		double t = v * ( 1 - s * ( 1 - remainder ) );

		if( h_int < 1 ) {
			rgb[0] = v;
			rgb[1] = t;
			rgb[2] = p;
		} else if( h_int < 2 ) {
			rgb[0] = q;
			rgb[1] = v;
			rgb[2] = p;
		} else if( h_int < 3 ) {
			rgb[0] = p;
			rgb[1] = v;
			rgb[2] = t;
		} else if( h_int < 4 ) {
			rgb[0] = p;
			rgb[1] = q;
			rgb[2] = v;
		} else if( h_int < 5 ) {
			rgb[0] = t;
			rgb[1] = p;
			rgb[2] = v;
		} else {
			rgb[0] = v;
			rgb[1] = p;
			rgb[2] = q;
		}
	}
554948
private void RGBtoHSV(float red, float green, float blue) {

        float min = 0;
        float max = 0;
        float delta = 0;

        min = MIN(red, green, blue);
        max = MAX(red, green, blue);

        m_bri = max; // v

        delta = max - min;

        if (max != 0) {
            m_sat = delta / max; // s
        } else {
            m_sat = 0;
            m_hue = 0;
            return;
        }

        if (delta == 0) {
            m_hue = 0;
            return;
        }

        if (red == max) {
            m_hue = (green - blue) / delta;
        } else if (green == max) {
            m_hue = 2 + ((blue - red) / delta);
        } else {
            m_hue = 4 + ((red - green) / delta);
        }

        m_hue *= 60;

        if (m_hue < 0) {
            m_hue += 360;
        }
    }
398563
def convert_l_to_rgba(self, row, result):
        
        for i in range(len(row) // 3):
            for j in range(3):
                result[(4 * i) + j] = row[i]
901030
def _rgb2l(self, mode):
        
        self._check_modes(("RGB", "RGBA"))

        kb_ = 0.114
        kr_ = 0.299

        r__ = self.channels[0]
        g__ = self.channels[1]
        b__ = self.channels[2]

        y__ = kr_ * r__ + (1 - kr_ - kb_) * g__ + kb_ * b__

        if self.fill_value is not None:
            self.fill_value = ([rgb2ycbcr(self.fill_value[0],
                                          self.fill_value[1],
                                          self.fill_value[2])[0]] +
                               self.fill_value[3:])

        self.channels = [y__] + self.channels[3:]

        self.mode = mode
228927
public static int[] RGBtoHSV (float r, float g, float b) {
		float h, s, v;
		float min, max, delta;

		min = Math.min(Math.min(r, g), b);
		max = Math.max(Math.max(r, g), b);
		v = max;

		delta = max - min;

		if (max != 0)
			s = delta / max;
		else {
			s = 0;
			h = 0;
			return new int[]{MathUtils.round(h), MathUtils.round(s), MathUtils.round(v)};
		}

		if (delta == 0)
			h = 0;
		else {

			if (r == max)
				h = (g - b) / delta;
			else if (g == max)
				h = 2 + (b - r) / delta;
			else
				h = 4 + (r - g) / delta;
		}

		h *= 60;
		if (h < 0)
			h += 360;

		s *= 100;
		v *= 100;

		return new int[]{MathUtils.round(h), MathUtils.round(s), MathUtils.round(v)};
	}
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;
  }
433430
def _rgb_to_rgbw(color):
        
        # Get the maximum between R, G, and B
        max_value = max(color)

        # If the maximum value is 0, immediately return pure black.
        if max_value == 0:
            return [0] * 4

        # Figure out what the color with 100% hue is
        multiplier = 255 / max_value
        hue_red = color.R * multiplier
        hue_green = color.G * multiplier
        hue_blue = color.B * multiplier

        # Calculate the whiteness (not strictly speaking luminance)
        max_hue = max(hue_red, hue_green, hue_blue)
        min_hue = min(hue_red, hue_green, hue_blue)
        luminance = ((max_hue + min_hue) / 2 - 127.5) * 2 / multiplier

        # Calculate the output values
        r = max(0, min(255, int(color.R - luminance)))
        g = max(0, min(255, int(color.G - luminance)))
        b = max(0, min(255, int(color.B - luminance)))
        w = max(0, min(255, int(luminance)))

        return [r, g, b, w]
706421
def ratio(rgb1, rgb2)
      raise InvalidColorError, rgb1 unless valid_rgb?(rgb1)
      raise InvalidColorError, rgb2 unless valid_rgb?(rgb2)

      srgb1 = rgb_to_srgba(rgb1)
      srgb2 = rgb_to_srgba(rgb2)

      l1 = srgb_lightness(srgb1)
      l2 = srgb_lightness(srgb2)

      l1 > l2 ? (l1 + 0.05) / (l2 + 0.05) : (l2 + 0.05) / (l1 + 0.05)
    end
1184114
def hsla_to_rgba(h, s, l, a):
    
    h = h % 360
    s = max(0, min(1, s))
    l = max(0, min(1, l))
    a = max(0, min(1, a))

    c = (1 - abs(2*l - 1)) * s
    x = c * (1 - abs(h/60%2 - 1))
    m = l - c/2

    if h<60:
        r, g, b = c, x, 0
    elif h<120:
        r, g, b = x, c, 0
    elif h<180:
        r, g, b = 0, c, x
    elif h<240:
        r, g, b = 0, x, c
    elif h<300:
        r, g, b = x, 0, c
    else:
        r, g, b = c, 0, x


    return (int((r+m)*255), int((g+m)*255), int((b+m)*255), int(a*255))
554458
public static void srgbToXyz( double r , double g , double b , double xyz[] ) {
		xyz[0] = 0.412453*r + 0.35758*g + 0.180423*b;
		xyz[1] = 0.212671*r + 0.71516*g + 0.072169*b;
		xyz[2] = 0.019334*r + 0.119193*g + 0.950227*b;
	}
1001623
def as_rgb(self):
        

        html = self.as_html()

        return (
            int(html[1:3], 16),
            int(html[3:5], 16),
            int(html[5:7], 16)
        )
35303
function rgbToHsl(rgb) {
		var r, g, b, max, min, d, h, s, l,
			cache,
			del_R, del_G, del_B;

		rgb = ColorHelper.hexToRGB(rgb);
		r = rgb[0] / 255;
		g = rgb[1] / 255;
		b = rgb[2] / 255;
		max = Math.max(r, g, b);
		min = Math.min(r, g, b);

		// Delta RGB value
		d = max - min;
		l = (max + min) / 2;

		// gray?, no chroma...
		if (d === 0) {
			// HSl results from 0 to 1
			h = 0;
			s = 0;
		} else {
			// Chromatic data...
			s = d / (l < 0.5 ? (max + min) : (2 - max - min));

			del_R = (((max - r) / 6) + (d / 2)) / d;
			del_G = (((max - g) / 6) + (d / 2)) / d;
			del_B = (((max - b) / 6) + (d / 2)) / d;

			if (r === max) {
				h = del_B - del_G;
			} else if (g === max) {
				h = (1 / 3) + del_R - del_B;
			} else if (b === max) {
				h = (2 / 3) + del_G - del_R;
			}

			if (h < 0) {
				h += 1;
			}
			if (h > 0) {
				h -= 1;
			}
		}

		h = Math.round(h * 360);
		if (h < 0) {
			h += 360;
		}

		cache = colorCache(this);
		cache.hsl = [h, Math.round(s * 100), Math.round(l * 100)];
		return cache.hsl;
	}
701039
def RGB_to_HSL(cobj, *args, **kwargs):
    
    var_R = cobj.rgb_r
    var_G = cobj.rgb_g
    var_B = cobj.rgb_b

    var_max = max(var_R, var_G, var_B)
    var_min = min(var_R, var_G, var_B)

    var_H = __RGB_to_Hue(var_R, var_G, var_B, var_min, var_max)
    var_L = 0.5 * (var_max + var_min)

    if var_max == var_min:
        var_S = 0
    elif var_L <= 0.5:
        var_S = (var_max - var_min) / (2.0 * var_L)
    else:
        var_S = (var_max - var_min) / (2.0 - (2.0 * var_L))

    return HSLColor(
        var_H, var_S, var_L)
509662
def HSL_to_RGB(cobj, target_rgb, *args, **kwargs):
    
    H = cobj.hsl_h
    S = cobj.hsl_s
    L = cobj.hsl_l

    if L < 0.5:
        var_q = L * (1.0 + S)
    else:
        var_q = L + S - (L * S)

    var_p = 2.0 * L - var_q

    # H normalized to range [0,1]
    h_sub_k = (H / 360.0)

    t_sub_R = h_sub_k + (1.0 / 3.0)
    t_sub_G = h_sub_k
    t_sub_B = h_sub_k - (1.0 / 3.0)

    rgb_r = __Calc_HSL_to_RGB_Components(var_q, var_p, t_sub_R)
    rgb_g = __Calc_HSL_to_RGB_Components(var_q, var_p, t_sub_G)
    rgb_b = __Calc_HSL_to_RGB_Components(var_q, var_p, t_sub_B)

    # TODO: Investigate intent of following code block.
    # In the event that they define an HSV color and want to convert it to
    # a particular RGB space, let them override it here.
    # if target_rgb is not None:
    #     rgb_type = target_rgb
    # else:
    #     rgb_type = cobj.rgb_type

    return target_rgb(rgb_r, rgb_g, rgb_b)
123651
def _hex_to_rgb(color):
    
    if color[0] == '#':
        color = color[1:]
    if len(color) == 3:
        color = color[0] * 2 + color[1] * 2 + color[2] * 2
    if len(color) != 6:
        raise ValueError('Input #{0} is not in #RRGGBB format'.format(color))
    return [int(n, 16) for n in (color[:2], color[2:4], color[4:])]