Skip to content

Commit

Permalink
fix(utils): support short hex
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyv committed Jan 30, 2024
1 parent 75027f4 commit 29aed45
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,14 @@ export function hslToHsb(color: string): string {

// HEX To Others
export function hexToRgb(color: string): string {
const match = color.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i)
if (!match)
let match
const _match = color.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i)
const _shortMatch = color.match(/^#?([a-f\d])([a-f\d])([a-f\d])$/i)
if (_match)
match = _match
else if (_shortMatch)
match = _shortMatch
else
throw new Error('Invalid HEX color format.')

const r = Number.parseInt(match[1], 16)
Expand All @@ -249,8 +255,14 @@ export function hexToRgb(color: string): string {
}

export function hexToHsl(color: string): string {
const match = color.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i)
if (!match)
let match
const _match = color.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i)
const _shortMatch = color.match(/^#?([a-f\d])([a-f\d])([a-f\d])$/i)
if (_match)
match = _match
else if (_shortMatch)
match = _shortMatch
else
throw new Error('Invalid HEX color format.')

const r = Number.parseInt(match[1], 16) / 255
Expand Down Expand Up @@ -284,8 +296,14 @@ export function hexToHsl(color: string): string {
}

export function hexToHsb(color: string): string {
const match = color.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i)
if (!match)
let match
const _match = color.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i)
const _shortMatch = color.match(/^#?([a-f\d])([a-f\d])([a-f\d])$/i)
if (_match)
match = _match
else if (_shortMatch)
match = _shortMatch
else
throw new Error('Invalid HEX color format.')

const r = Number.parseInt(match[1], 16)
Expand Down

0 comments on commit 29aed45

Please sign in to comment.