hwb2rgb static method

List<int> hwb2rgb(
  1. num h,
  2. num whiteness,
  3. num blackness
)

Implementation

static List<int> hwb2rgb(num h, num whiteness, num blackness) {
  if (whiteness <= 1 && blackness <= 1) {
    // if both <=1 we assume that are 0-1 range, NOTE that means
    // if REALLY want (0.01 whiteness AND blackness then you need to pass it like that
    //   and not as 1 in 1-100 scale)
    //  (because passing both as 1 will assume 0 to 1 scale, NOT 1 in the 100 scale)
    whiteness *= 100;
    blackness *= 100;
  }
  h = h / 360;
  var wh = whiteness / 100;
  var bl = blackness / 100;
  var ratio = wh + bl;
  num f;

  // Wh + bl cant be > 1
  if (ratio > 1) {
    wh /= ratio;
    bl /= ratio;
  }

  var i = (6 * h).floor();
  var v = 1 - bl;
  f = 6 * h - i;

  if ((i & 0x01) != 0) {
    f = 1 - f;
  }

  var n = wh + f * (v - wh); // Linear interpolation

  num r;
  num g;
  num b;

  switch (i) {
    case 1:
      r = n;
      g = v;
      b = wh;
      break;
    case 2:
      r = wh;
      g = v;
      b = n;
      break;
    case 3:
      r = wh;
      g = n;
      b = v;
      break;
    case 4:
      r = n;
      g = wh;
      b = v;
      break;
    case 5:
      r = v;
      g = wh;
      b = n;
      break;
    case 6:
    case 0:
    default:
      r = v;
      g = n;
      b = wh;
      break;
  }
  return [(r * 255).round(), (g * 255).round(), (b * 255).round()];
}