From 55b5e98aed7245d0c04f4d8904d30ac76159b5c9 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Sun, 18 Jul 2021 14:38:24 +0800 Subject: [PATCH] feat(color-convert): add hsva to hexa --- packages/color-convert/README.md | 6 ++++++ packages/color-convert/src/index.ts | 1 + test/convert.test.tsx | 11 ++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/color-convert/README.md b/packages/color-convert/README.md index d6cf7cbb6..9118fbe27 100644 --- a/packages/color-convert/README.md +++ b/packages/color-convert/README.md @@ -35,6 +35,12 @@ hexToHsva("#ffffff") //=> { h: 0, s: 0, v: 100, a: 1 } hsvaToHex({ h: 0, s: 0, v: 100, a: 1 }) // => "#ffffff" ``` +#### `hsvaToHexa` + +```js +hsvaToHex({ h: 0, s: 0, v: 100, a: 1 }) // => "#ffffff" +``` + #### `hsvaToHsla` ```js diff --git a/packages/color-convert/src/index.ts b/packages/color-convert/src/index.ts index 034c13493..a26a3a187 100644 --- a/packages/color-convert/src/index.ts +++ b/packages/color-convert/src/index.ts @@ -271,6 +271,7 @@ export const hsvaToRgbaString = (hsva: HsvaColor): string => { export const rgbaToRgb = ({ r, g, b }: RgbaColor): RgbColor => ({ r, g, b }); export const hslaToHsl = ({ h, s, l }: HslaColor): HslColor => ({ h, s, l }); export const hsvaToHex = (hsva: HsvaColor): string => rgbaToHex(hsvaToRgba(hsva)); +export const hsvaToHexa = (hsva: HsvaColor): string => rgbaToHexa(hsvaToRgba(hsva)); export const hsvaToHsv = ({ h, s, v }: HsvaColor): HsvColor => ({ h, s, v }); export const color = (str: string | HsvaColor): ColorResult => { diff --git a/test/convert.test.tsx b/test/convert.test.tsx index a92e31a1e..575851c82 100644 --- a/test/convert.test.tsx +++ b/test/convert.test.tsx @@ -1,6 +1,6 @@ import { color, getContrastingColor } from '../packages/color-convert/src'; // HEX -import { hexToHsva, hexToRgba, hsvaToHex } from '../packages/color-convert/src'; +import { hexToHsva, hexToRgba, hsvaToHex, hsvaToHexa } from '../packages/color-convert/src'; import { equalHex } from '../packages/color-convert/src/utils'; // HSLA import { hsvaToHsla, hslaToHsva, HsvaColor, HslaColor } from '../packages/color-convert/src'; @@ -125,6 +125,15 @@ it('Converts HSVA to HEX', () => { expect(hsvaToHex({ h: 284, s: 93, v: 73, a: 1 })).toBe('#8c0dba'); }); +it('Converts HSVA to HEXA', () => { + expect(hsvaToHexa({ h: 0, s: 0, v: 30.19607843137255, a: 0.4875 })).toBe('#4d4d4d7c'); + expect(hsvaToHexa({ h: 0, s: 0, v: 100, a: 1 })).toBe('#ffffffff'); + expect(hsvaToHexa({ h: 60, s: 100, v: 100, a: 1 })).toBe('#ffff00ff'); + expect(hsvaToHexa({ h: 0, s: 100, v: 100, a: 1 })).toBe('#ff0000ff'); + expect(hsvaToHexa({ h: 0, s: 0, v: 0, a: 1 })).toBe('#000000ff'); + expect(hsvaToHexa({ h: 284, s: 93, v: 73, a: 1 })).toBe('#8c0dbaff'); +}); + it('Converts HSVA to HSLA', () => { let test = (input: HsvaColor, output: HslaColor) => expect(hsvaToHsla(input)).toMatchObject(output); test({ h: 0, s: 0, v: 100, a: 1 }, { h: 0, s: 0, l: 100, a: 1 });