hslaToHex function

String hslaToHex(
  1. String hsla
)

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

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

Parameters:

  • hsla: The HSLA color string to convert to hexadecimal format.

Returns: The converted color string in hexadecimal format.

Example:

print(hslaToHex('hsla(0, 100%, 50%, 0.5)')); // Output: #ff000080

Implementation

String hslaToHex(String hsla) {
  hsla = hsla.replaceAll('hsla(', '').replaceAll(')', '');
  List<String> hslaValues = hsla.split(',');
  double h = double.parse(hslaValues[0].trim());
  double s = double.parse(hslaValues[1].replaceAll('%', '').trim()) / 100;
  double l = double.parse(hslaValues[2].replaceAll('%', '').trim()) / 100;
  double a = double.parse(hslaValues[3].trim());
  int alpha = (a * 255).round();
  List<int> rgb = _hslToRgb(h, s, l);
  return _toHex(rgb[0], rgb[1], rgb[2], alpha);
}