Convert HSL string to array

javascripthsl
Sep 17, 2022

JavaScript

const toHSLArray = (hslStr) => {
  const hslArr = hslStr.match(/\d+/g)?.map(Number)
  if (hslArr && hslArr.length >= 3) {
    return [hslArr[0], hslArr[1], hslArr[2]]
  }
}

TypeScript

export const toHSLArray = (
  hslStr: string
): [number, number, number] | undefined => {
  const hslArr = hslStr.match(/\d+/g)?.map(Number)
  if (hslArr && hslArr.length >= 3) {
    return [hslArr[0], hslArr[1], hslArr[2]]
  }
}

Example

toHSLArray('hsla(241, 77%, 54%, 1)')
> [241, 77, 54]

Till next time, take care ✌

Related Articles

How to Use Lucide Icons with Vuetify

A guide on integrating Lucide icons into a Vuetify project.

Dexie upgrade function is not called

While creating a new db version, upgrade is not called

Getting epoch time

Getting epoch time in various languages