Average the values of two colors together
769321
def average_colors(c1, c2):
    '''  '''
    r = int((c1[0] + c2[0])/2)
    g = int((c1[1] + c2[1])/2)
    b = int((c1[2] + c2[2])/2)

    return (r, g, b)
1193316
def _cal_color(self, value, color_index):
        """"""
        range_min_p = self._domain[color_index]
        range_p = self._domain[color_index + 1] - range_min_p
        try:
            factor = (value - range_min_p) / range_p
        except ZeroDivisionError:
            factor = 0

        min_color = self.colors[color_index]
        max_color = self.colors[color_index + 1]
        red = round(factor * (max_color.r - min_color.r) + min_color.r)
        green = round(factor * (max_color.g - min_color.g) + min_color.g)
        blue = round(factor * (max_color.b - min_color.b) + min_color.b)

        return Color(red, green, blue)
399662
function(color1, color2, factor) {
		if (arguments.length < 3) { factor = 0.5; }
		var hsl1 = this.rgb2hsl(color1);
		var hsl2 = this.rgb2hsl(color2);
		for (var i=0;i<3;i++) {
			hsl1[i] += factor*(hsl2[i]-hsl1[i]);
		}
		return this.hsl2rgb(hsl1);
	}
463058
def hue_similarity( color1, color2 )
      deg1   = color1.to_hsl.h
      deg2   = color2.to_hsl.h
      deg1  += 1 if deg1 < 0
      deg2  += 1 if deg2 < 0
      delta  = (deg1 - deg2).abs
      delta  = 1 - delta if delta > 0.5
      delta * 360
    end
857706
function make_color(aggregates, all, leaf_only) {
    let colorRange;
    if (aggregates.length >= 2) {
        colorRange = [Infinity, -Infinity];
        for (let series of all) {
            let colorvals = series["data"];
            for (let i = 1; i < colorvals.length; ++i) {
                if ((leaf_only && colorvals[i].leaf) || !leaf_only) {
                    colorRange[0] = Math.min(colorRange[0], colorvals[i].colorValue);
                    colorRange[1] = Math.max(colorRange[1], colorvals[i].colorValue);
                }
            }
            if (colorRange[0] * colorRange[1] < 0) {
                let cmax = Math.max(Math.abs(colorRange[0]), Math.abs(colorRange[1]));
                colorRange = [-cmax, cmax];
            }
        }
    }
    return colorRange;
}
1191713
def mix(color1, color2, pos=0.5):
    
    opp_pos = 1 - pos

    red = color1[0] * pos + color2[0] * opp_pos
    green = color1[1] * pos + color2[1] * opp_pos
    blue = color1[2] * pos + color2[2] * opp_pos
    return int(red), int(green), int(blue)
799072
function averageColorNormalized() {
	for (var _len = arguments.length, colors = Array(_len), _key = 0; _key < _len; _key++) {
		colors[_key] = arguments[_key];
	}

	// If a single colors array was provided as opposed to separate arguments
	// for each color, break it out.
	if (colors.length === 1 && Array.isArray(colors[0][0])) colors = colors[0];

	var N = colors.length;

	// Normalize the color components to range [0, 1].
	var normalizedColors = colors.map(function (_ref) {
		var _ref2 = _slicedToArray(_ref, 3),
		    h = _ref2[0],
		    s = _ref2[1],
		    l = _ref2[2];

		return [h / 360, s / 100, l / 100];
	});

	var cartesianAverage = normalizedColors
	// Cylindrical => Cartesian coordinates.
	.map(function (_ref3) {
		var _ref4 = _slicedToArray(_ref3, 3),
		    h = _ref4[0],
		    s = _ref4[1],
		    l = _ref4[2];

		return [s * cos(h * PI2), s * sin(h * PI2), l];
	})
	// Sum the scaled coordinates to find a centerpoint.
	.reduce(function (_ref5, _ref6) {
		var _ref8 = _slicedToArray(_ref5, 3),
		    xA = _ref8[0],
		    yA = _ref8[1],
		    zA = _ref8[2];

		var _ref7 = _slicedToArray(_ref6, 3),
		    x = _ref7[0],
		    y = _ref7[1],
		    z = _ref7[2];

		return [xA + x / N, yA + y / N, zA + z / N];
	}, [0, 0, 0]);

	// Convert the average hue value back to a positive polar angle.
	var H = (atan2(cartesianAverage[Y], cartesianAverage[X]) / PI2 + 1) % 1;

	// Take the arithmetic mean of all saturation values.
	var S = normalizedColors.map(function (_ref9) {
		var _ref10 = _slicedToArray(_ref9, 3),
		    h = _ref10[0],
		    s = _ref10[1],
		    l = _ref10[2];

		return s / N;
	}).reduce(sum);

	// Take the arithmetic mean of all lightness values.
	var L = cartesianAverage[2];

	return [H, S, L];
}
1318125
def _mix_color(slice1, slice2, alpha_channels, color_space):
    """"""

    if slice1.shape != slice2.shape:
        raise ValueError('size mismatch between cropped slices and checkers!!!')

    alpha_channels = np.array(alpha_channels)
    if len(alpha_channels) != 2:
        raise ValueError('Alphas must be two value tuples.')

    slice1, slice2 = scale_images_0to1(slice1, slice2)

    # masking background
    combined_distr = np.concatenate((slice1.flatten(), slice2.flatten()))
    image_eps = np.percentile(combined_distr, 5)
    background = np.logical_or(slice1 <= image_eps, slice2 <= image_eps)

    if color_space.lower() in ['rgb']:

        red = alpha_channels[0] * slice1
        grn = alpha_channels[1] * slice2
        blu = np.zeros_like(slice1)

        # foreground = np.logical_not(background)
        # blu[foreground] = 1.0

        mixed = np.stack((red, grn, blu), axis=2)

    elif color_space.lower() in ['hsv']:

        raise NotImplementedError(
            'This method (color_space="hsv") is yet to fully conceptualized and implemented.')

        # TODO other ideas: hue/saturation/intensity value driven by difference in intensity?
        hue = alpha_channels[0] * slice1
        sat = alpha_channels[1] * slice2
        val = np.ones_like(slice1)

        hue[background] = 1.0
        sat[background] = 0.0
        val[background] = 0.0

        mixed = np.stack((hue, sat, val), axis=2)
        # converting to RGB
        mixed = mpl.colors.hsv_to_rgb(mixed)

    # ensuring all values are clipped to [0, 1]
    mixed[mixed <= 0.0] = 0.0
    mixed[mixed >= 1.0] = 1.0

    return mixed
1169532
def normalize_rgb(r, g, b, a):
    
    r = int(r, 10)
    g = int(g, 10)
    b = int(b, 10)
    if a:
        a = float(a) * 256
    if r > 255 or g > 255 or b > 255 or (a and a > 255):
        return None
    color = '#%02x%02x%02x' % (r, g, b)
    if a:
        color += '%02x' % int(a)
    return color
229895
def parse(self, color):
        """"""
        assert color is not None

        #parse color into rgb values
        if isinstance(color, str):
            match = self.hex_color_long.match(color)
            if match:
                color = [int(color, 16) / 65535.0 for color in match.groups()]
            else:
                match = self.hex_color_normal.match(color)
                if match:
                    color = [int(color, 16) / 255.0 for color in match.groups()]
                else:
                    match = self.hex_color_short.match(color)
                    color = [int(color + color, 16) / 255.0 for color in match.groups()]

        elif isinstance(color, gdk.Color):
            color = [color.red / 65535.0,
                     color.green / 65535.0,
                     color.blue / 65535.0]

        elif isinstance(color, (list, tuple)):
            # otherwise we assume we have color components in 0..255 range
            if color[0] > 1 or color[1] > 1 or color[2] > 1:
                color = [c / 255.0 for c in color]
        else:
            color = [color.red, color.green, color.blue]


        return color
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;
        }
    }
184035
function (color1, color2, weight) {
        if (!weight) {
            weight = new(tree.Dimension)(50);
        }
        var p = weight.value / 100.0;
        var w = p * 2 - 1;
        var a = color1.toHSL().a - color2.toHSL().a;

        var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0;
        var w2 = 1 - w1;

        var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2,
                   color1.rgb[1] * w1 + color2.rgb[1] * w2,
                   color1.rgb[2] * w1 + color2.rgb[2] * w2];

        var alpha = color1.alpha * p + color2.alpha * (1 - p);

        return new(tree.Color)(rgb, alpha);
    }
937269
def _avg(value1, value2, weight):
        
        if value1 is None:
            return value2
        if value2 is None:
            return value1
        return value2 * weight + value1 * (1 - weight)
105637
function(env, op, other) {
        var result = [],
            rgb2;

        if (other instanceof tree.Color) {
            rgb2 = chroma(other.toString()).rgb();
        }
        else if (_.isArray(other)) {
            rgb2 = _.slice(other, 0, Math.max(other.length, 3));
        }
        else if (_.isObject(other)) {
            if (_.has(other, 'value')) {
                rgb2 = [other.value, other.value, other.value];
            }
            else {
                return;
            }
        }
        else {
            rgb2 = [other, other, other];
        }

        var rgb1 = chroma(this.toString()).rgb();

        for (var c = 0; c < 3; c++) {
            result[c] = tree.operate(op, rgb1[c] , rgb2[c]);
        }

        if (this.perceptual) {
            var normalize = function (x) {
                return x / 255;
            };
            result = hsluv.rgbToHsluv(_.map(result, normalize));
            result[1] = result[1] / 100;
            result[2] = result[2] / 100;
        }
        else {
            result = chroma(result).hsl();
        }

        return new tree.Color(result, this.alpha, this.perceptual);
    }
412188
private double doubleValue2Colors( double left, double right ) {
        return rgba( doubleValue( red( left ), red( right ) ), //
                        doubleValue( green( left ), green( right ) ), //
                        doubleValue( blue( left ), blue( right ) ), 1 );
    }
288511
function _colorSort(a, b) {
        if (a.count === b.count) {
            return 0;
        }
        if (a.count > b.count) {
            return -1;
        }
        if (a.count < b.count) {
            return 1;
        }
    }
492682
static double hsva( double hue, double saturation, double value, double alpha ) {
        hue = ((hue % 360) / 360) * 360;

        int i = (int)Math.floor( (hue / 60) % 6 );
        double f = (hue / 60) - i;

        double[] vs = { value, value * (1 - saturation), value * (1 - f * saturation), value * (1 - (1 - f) * saturation) };

        return rgba( vs[HSVA_PERM[i][0]] * 255, vs[HSVA_PERM[i][1]] * 255, vs[HSVA_PERM[i][2]] * 255, alpha );
    }