Skip to content

Commit

Permalink
fix(utils): short hex convert right
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyv committed Jan 30, 2024
1 parent e598002 commit 2ad6f87
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export function hexToRgb(color: string): string {
if (_match)
match = _match
else if (_shortMatch)
match = _shortMatch
match = [null, ..._shortMatch.slice(1).map(c => c + c)] as unknown as RegExpMatchArray
else
throw new Error('Invalid HEX color format.')

Expand All @@ -261,7 +261,7 @@ export function hexToHsl(color: string): string {
if (_match)
match = _match
else if (_shortMatch)
match = _shortMatch
match = [null, ..._shortMatch.slice(1).map(c => c + c)] as unknown as RegExpMatchArray
else
throw new Error('Invalid HEX color format.')

Expand Down Expand Up @@ -302,7 +302,7 @@ export function hexToHsb(color: string): string {
if (_match)
match = _match
else if (_shortMatch)
match = _shortMatch
match = [null, ..._shortMatch.slice(1).map(c => c + c)] as unknown as RegExpMatchArray
else
throw new Error('Invalid HEX color format.')

Expand Down
4 changes: 3 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { describe, expect, it } from 'vitest'
import { convertColor, hexToHsb, hexToHsl, hexToRgb, hsbToHex, hsbToHsl, hsbToRgb, hslToHex, hslToHsb, hslToRgb, isColor, rgbToHex, rgbToHsb, rgbToHsl } from '../src/index'
import { convertColor, hexToHsb, hexToHsl, hexToRgb, hsbToHex, hsbToHsl, hsbToRgb, hslToHex, hslToHsb, hslToRgb, isColor, rgbToHex, rgbToHsb, rgbToHsl } from '../src'
import type { ColorType } from '../src/types'

describe('utils scoped', () => {
const hex = '#ff0000'
const shortHex = '#f00'
const rgb = 'rgb(255, 0, 0)'
const hsl = 'hsl(0, 100%, 50%)'
const hsb = 'hsb(0, 100%, 100%)'
Expand All @@ -30,6 +31,7 @@ describe('utils scoped', () => {
expect(hexToHsb(hex)).toEqual(hsb)
expect(hexToHsl(hex)).toEqual(hsl)
expect(hexToRgb(hex)).toEqual(rgb)
expect(hexToRgb(shortHex)).toEqual(rgb)

// hsl to others test case
expect(hslToRgb(hsl)).toEqual(rgb)
Expand Down

0 comments on commit 2ad6f87

Please sign in to comment.