toHexString method

String toHexString (Vector4 input, { bool alpha: false, bool short: false })

Convert a input color to a hex string without a leading '#'. To include the alpha channel, set alpha to true, it is false by default. If short is true, the resulting hex string might also be a short version, like #ff0 (default false).

Implementation

static String toHexString(Vector4 input,
    {bool alpha: false, bool short: false}) {
  final int r = (input.r * 0xFF).floor() & 0xFF;
  final int g = (input.g * 0xFF).floor() & 0xFF;
  final int b = (input.b * 0xFF).floor() & 0xFF;
  final int a = (input.a * 0xFF).floor() & 0xFF;

  final bool isShort = short &&
      ((r >> 4) == (r & 0xF)) &&
      ((g >> 4) == (g & 0xF)) &&
      ((b >> 4) == (b & 0xF)) &&
      (!alpha || (a >> 4) == (a & 0xF));

  if (isShort) {
    final String rgb = (r & 0xF).toRadixString(16) +
        (g & 0xF).toRadixString(16) +
        (b & 0xF).toRadixString(16);

    return alpha ? (a & 0xF).toRadixString(16) + rgb : rgb;
  } else {
    final String rgb = r.toRadixString(16).padLeft(2, '0') +
        g.toRadixString(16).padLeft(2, '0') +
        b.toRadixString(16).padLeft(2, '0');

    return alpha ? a.toRadixString(16).padLeft(2, '0') + rgb : rgb;
  }
}