toHexString static method

String toHexString(
  1. Vector4 input, {
  2. bool alpha = false,
  3. 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 r = (input.r * 0xFF).floor() & 0xFF;
  final g = (input.g * 0xFF).floor() & 0xFF;
  final b = (input.b * 0xFF).floor() & 0xFF;
  final a = (input.a * 0xFF).floor() & 0xFF;

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

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

    return alpha ? (a & 0xF).toRadixString(16) + rgb : rgb;
  } else {
    final 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;
  }
}