toCSS method

  1. @override
String toCSS(
  1. Contexts context
)
override

Returns this color as string. Transparent, #rrggbb, #rgb.

Implementation

@override
String toCSS(Contexts context) {
  // final bool doNotCompress = false; // in js is an argument in toCSS. ??
  if (cleanCss?.compatibility?.properties?.colors ?? false) {
    return toCleanCSS(context);
  }

  final alphaFormatter = AlphaFormatter(context)
    ..removeLeadingZero = cleanCss != null;
  final numberFormatter = NumberFormatter()
    ..precision = context?.numPrecision;

  final compress = context?.compress ?? false; // && !doNotCompress;
  final _alpha = alphaFormatter.adjustPrecision(alpha);

  // `value` is set if this color was originally
  // converted from a named color string so we need
  // to respect this and try to output named color too.

  String colorFunction;
  if (value != null) {
    if (value.startsWith('rgb')) {
      if (_alpha < 1) colorFunction = 'rgba';
    } else if (value.startsWith('hsl')) {
      colorFunction = _alpha < 1 ? 'hsla' : 'hsl';
    } else {
      return value;
    }
  } else {
    if (_alpha < 1) colorFunction = 'rgba';
  }

  List<String> args;
  switch (colorFunction) {
    case 'rgba':
      args = [
        for (var c in rgb) c.round().toString(),
        alphaFormatter.format(_alpha, formatted: true)
      ];
      break;
    case 'hsla':
      args = <String>[alphaFormatter.format(_alpha, formatted: true)];
      continue hsl;
    hsl:
    case 'hsl':
      final color = toHSL();
      args = <String>[
        numberFormatter.format(color.h),
        '${numberFormatter.format(color.s * 100)}%',
        '${numberFormatter.format(color.l * 100)}%',
        ...?args,
      ];
      break;
  }

  // Values are capped between `0` and `255`, rounded and zero-padded.
  if (colorFunction != null) {
    final separator = _isCompress(context) ? ',' : ', ';
    return '$colorFunction(${args.join(separator)})';
  }

  final color = toRGB();
  return compress ? tryHex3(color) : color;

// 3.8.0 20180808
//  Color.prototype.toCSS = function (context, doNotCompress) {
//      var compress = context && context.compress && !doNotCompress, color, alpha,
//          colorFunction, args = [];
//
//      // `value` is set if this color was originally
//      // converted from a named color string so we need
//      // to respect this and try to output named color too.
//      alpha = this.fround(context, this.alpha);
//
//      if (this.value) {
//          if (this.value.indexOf('rgb') === 0) {
//              if (alpha < 1) {
//                  colorFunction = 'rgba';
//              }
//          } else if (this.value.indexOf('hsl') === 0) {
//              if (alpha < 1) {
//                  colorFunction = 'hsla';
//              } else {
//                  colorFunction = 'hsl';
//              }
//          } else {
//              return this.value;
//          }
//      } else {
//          if (alpha < 1) {
//              colorFunction = 'rgba';
//          }
//      }
//
//      switch (colorFunction) {
//          case 'rgba':
//              args = this.rgb.map(function (c) {
//                  return clamp(Math.round(c), 255);
//              }).concat(clamp(alpha, 1));
//              break;
//          case 'hsla':
//              args.push(clamp(alpha, 1));
//          case 'hsl':
//              color = this.toHSL();
//              args = [
//                  this.fround(context, color.h),
//                  this.fround(context, color.s * 100) + '%',
//                  this.fround(context, color.l * 100) + '%'
//              ].concat(args);
//      }
//
//      if (colorFunction) {
//          // Values are capped between `0` and `255`, rounded and zero-padded.
//          return colorFunction + '(' + args.join(',' + (compress ? '' : ' ')) + ')';
//      }
//
//      color = this.toRGB();
//
//      if (compress) {
//          var splitcolor = color.split('');
//
//          // Convert color to short format
//          if (splitcolor[1] === splitcolor[2] && splitcolor[3] === splitcolor[4] && splitcolor[5] === splitcolor[6]) {
//              color = '#' + splitcolor[1] + splitcolor[3] + splitcolor[5];
//          }
//      }
//
//      return color;
//  };
}