setStyle method

Color setStyle([
  1. String style = '',
  2. String colorSpace = SRGBColorSpace
])

Implementation

Color setStyle([String style = '', String colorSpace = SRGBColorSpace]) {
  void handleAlpha(String? string) {
    if (string == null) return;
    if (double.parse(string) < 1) {
      print('THREE.Color: Alpha component of $style will be ignored.');
    }
  }

  var _reg1 = RegExp(r"^\#([A-Fa-f\d]+)$");

  if (_reg1.hasMatch(style)) {
    var match = _reg1.firstMatch(style);
    var hex = match!.group(1)!;
    var size = hex.length;

    if (size == 3) {
      // #ff0
      r = int.parse(charAt(hex, 0) + charAt(hex, 0), radix: 16) / 255;
      g = int.parse(charAt(hex, 1) + charAt(hex, 1), radix: 16) / 255;
      b = int.parse(charAt(hex, 2) + charAt(hex, 2), radix: 16) / 255;

      return this;
    } else if (size == 6) {
      // #ff0000
      r = int.parse(charAt(hex, 0) + charAt(hex, 1), radix: 16) / 255;
      g = int.parse(charAt(hex, 2) + charAt(hex, 3), radix: 16) / 255;
      b = int.parse(charAt(hex, 4) + charAt(hex, 5), radix: 16) / 255;

      return this;
    }
  } else {
    var _reg2 = RegExp(r"^((?:rgb|hsl)a?)\(\s*([^\)]*)\)");

    if (_reg2.hasMatch(style)) {
      var match = _reg2.firstMatch(style)!;

      // print(" match.groupCount: ${match.groupCount} 1: ${match.group(1)} 2: ${match.group(2)} ");

      var name = match.group(1)!;
      var components = match.group(2)!;

      switch (name) {
        case 'rgb':
        case 'rgba':
          var _colorReg1 = RegExp(
              r"^(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d*\.?\d+)\s*)?$");
          if (_colorReg1.hasMatch(components)) {
            var match1 = _colorReg1.firstMatch(components)!;

            var c1 = match1.group(1)!;
            var c2 = match1.group(2)!;
            var c3 = match1.group(3)!;
            var c4 = match1.group(4);
            // rgb(255,0,0) rgba(255,0,0,0.5)
            r = Math.min(255, int.parse(c1, radix: 10)) / 255;
            g = Math.min(255, int.parse(c2, radix: 10)) / 255;
            b = Math.min(255, int.parse(c3, radix: 10)) / 255;

            ColorManagement.toWorkingColorSpace( this, colorSpace );

            handleAlpha(c4);

            return this;
          } else {
            var _colorReg2 = RegExp(
                r"^(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$");
            if (_colorReg2.hasMatch(components)) {
              var match2 = _colorReg2.firstMatch(components)!;

              var c1 = match2.group(1)!;
              var c2 = match2.group(2)!;
              var c3 = match2.group(3)!;
              var c4 = match2.group(4);

              // rgb(100%,0%,0%) rgba(100%,0%,0%,0.5)
              r = Math.min(100, int.parse(c1, radix: 10)) / 100;
              g = Math.min(100, int.parse(c2, radix: 10)) / 100;
              b = Math.min(100, int.parse(c3, radix: 10)) / 100;

              ColorManagement.toWorkingColorSpace( this, colorSpace );

              handleAlpha(c4);

              return this;
            }
          }

          break;

        case 'hsl':
        case 'hsla':
          var _colorReg3 = RegExp(
              r"^(\d*\.?\d+)\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$");
          if (_colorReg3.hasMatch(components)) {
            var match3 = _colorReg3.firstMatch(components)!;

            var c1 = match3.group(1)!;
            var c2 = match3.group(2)!;
            var c3 = match3.group(3)!;
            var c4 = match3.group(4);

            // hsl(120,50%,50%) hsla(120,50%,50%,0.5)
            var h = double.parse(c1) / 360;
            var s = int.parse(c2, radix: 10) / 100;
            var l = int.parse(c3, radix: 10) / 100;

            handleAlpha(c4);

            return setHSL(h, s, l, colorSpace);
          }

          break;
      }
    } else {
      if (style.isNotEmpty) {
        return setColorName(style);
      }
    }
  }

  // #ff0000
  // var hex = style.replaceAll("#", "");
  // var size = hex.length;

  // this.r = int.parse( hex[0] + hex[1], radix: 16 ) / 255;
  // this.g = int.parse( hex[2] + hex[3], radix: 16 ) / 255;
  // this.b = int.parse( hex[4] + hex[5], radix: 16 ) / 255;

  return this;
}