mix method

Color mix(
  1. Node color1,
  2. Node color2,
  3. [Dimension weight]
)

Mix two colors together in variable proportion. Opacity is included in the calculations.

Parameters: color1: A color object. color2: A color object. weight: Optional, a percentage balance point between the two colors, defaults to 50%. Returns: color Example: mix(#ff0000, #0000ff, 50%) mix(rgba(100,0,0,1.0), rgba(0,100,0,0.5), 50%) Output: #800080 rgba(75, 25, 0, 0.75)

Implementation

// Copyright (c) 2006-2009 Hampton Catlin, Natalie Weizenbaum, and Chris Eppstein
// http://sass-lang.com
Color mix(Node color1, Node color2, [Dimension weight]) {
  weight ??= Dimension(50);

  final p = weight.value / 100.0;
  final w = p * 2 - 1;
  final a = toHSL(color1).a - toHSL(color2).a;

  // we are sure now color1 and color2 are Color type
  final Color _color1 = color1;
  final Color _color2 = color2;

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

  final rgb = <num>[
    _color1.r * w1 + _color2.r * w2,
    _color1.g * w1 + _color2.g * w2,
    _color1.b * w1 + _color2.b * w2
  ];

  final double alpha = _color1.alpha * p + _color2.alpha * (1 - p);

  return Color.fromList(rgb, alpha);

// 3.9.0 20190711
//  mix: function (color1, color2, weight) {
//    if (!weight) {
//      weight = new Dimension(50);
//    }
//    var p = weight.value / 100.0;
//    var w = p * 2 - 1;
//    var a = toHSL(color1).a - toHSL(color2).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 Color(rgb, alpha);
//  },
}