hslToHex function

String hslToHex(
  1. String hsl
)

Parses an HSL color string to a valid hexadecimal color string.

Converts the HSL color format string hsl (e.g., 'hsl(0, 100%, 50%)') to its hexadecimal representation.

Parameters:

  • hsl: The HSL color string to convert to hexadecimal format.

Returns: The converted color string in hexadecimal format.

Example:

print(hslToHex('hsl(0, 100%, 50%)')); // Output: #ff0000

Implementation

String hslToHex(String hsl) {
  hsl = hsl.replaceAll('hsl(', '').replaceAll(')', '');
  List<String> hslValues = hsl.split(',');
  double h = double.parse(hslValues[0].trim());
  double s = double.parse(hslValues[1].replaceAll('%', '').trim()) / 100;
  double l = double.parse(hslValues[2].replaceAll('%', '').trim()) / 100;
  List<int> rgb = _hslToRgb(h, s, l);
  return _toHex(rgb[0], rgb[1], rgb[2], 255);
}