Skip to content

Commit 2fffbdb

Browse files
committed
Add HexAlphaColorPicker
1 parent 679bc4f commit 2fffbdb

File tree

8 files changed

+50
-8
lines changed

8 files changed

+50
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ We provide 12 additional color picker components for different color models, unl
8989
| Import | Value example |
9090
| --------------------------- | ---------------------------------- |
9191
| `{ HexColorPicker }` | `"#ffffff"` |
92+
| `{ HexAlphaColorPicker }` | `"#ffffff88"` |
9293
| `{ RgbColorPicker }` | `{ r: 255, g: 255, b: 255 }` |
9394
| `{ RgbaColorPicker }` | `{ r: 255, g: 255, b: 255, a: 1 }` |
9495
| `{ RgbStringColorPicker }` | `"rgb(255, 255, 255)"` |

demo/src/components/DevTools.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PickerPreview } from "./PickerPreview";
33
import {
44
// HEX
55
HexColorPicker,
6+
HexAlphaColorPicker,
67
// RGB
78
RgbColor,
89
RgbColorPicker,
@@ -33,6 +34,11 @@ export const DevTools = (): JSX.Element => {
3334
return (
3435
<div>
3536
<PickerPreview<string> title="HEX" PickerComponent={HexColorPicker} initialColor="#406090" />
37+
<PickerPreview<string>
38+
title="HEX Alpha"
39+
PickerComponent={HexAlphaColorPicker}
40+
initialColor="#40609088"
41+
/>
3642
<PickerPreview<RgbColor>
3743
title="RGB"
3844
PickerComponent={RgbColorPicker}

demo/src/components/PickerPreview.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
12
import React, { useState } from "react";
23
import Frame from "react-frame-component";
34
import { HexColorInput } from "../../../src";
@@ -33,9 +34,10 @@ export function PickerPreview<T extends AnyColor>({
3334
<Wrapper>
3435
<PickerComponent color={color} onChange={handleChange} />
3536
</Wrapper>
36-
{/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
37-
{/* @ts-ignore */}
38-
{title === "HEX" && <HexColorInput color={color} onChange={handleChange} prefixed alpha />}
37+
{title.startsWith("HEX") && (
38+
// @ts-ignore
39+
<HexColorInput color={color} onChange={handleChange} prefixed alpha />
40+
)}
3941
</PreviewDemo>
4042
<PreviewOutput>{JSON.stringify(color)}</PreviewOutput>
4143
</PreviewContainer>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from "react";
2+
3+
import { AlphaColorPicker } from "./common/AlphaColorPicker";
4+
import { ColorModel, ColorPickerBaseProps } from "../types";
5+
import { equalHex } from "../utils/compare";
6+
import { hexToHsva, hsvaToHex } from "../utils/convert";
7+
8+
const colorModel: ColorModel<string> = {
9+
defaultColor: "0001",
10+
toHsva: hexToHsva,
11+
fromHsva: hsvaToHex,
12+
equal: equalHex,
13+
};
14+
15+
export const HexAlphaColorPicker = (props: Partial<ColorPickerBaseProps<string>>): JSX.Element => (
16+
<AlphaColorPicker {...props} colorModel={colorModel} />
17+
);

src/components/HexColorPicker.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { hexToHsva, hsvaToHex } from "../utils/convert";
88
const colorModel: ColorModel<string> = {
99
defaultColor: "000",
1010
toHsva: hexToHsva,
11-
fromHsva: hsvaToHex,
11+
fromHsva: ({ h, s, v }) => hsvaToHex({ h, s, v, a: 1 }),
1212
equal: equalHex,
1313
};
1414

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Color picker components
22
export { HexColorPicker } from "./components/HexColorPicker";
3+
export { HexAlphaColorPicker } from "./components/HexAlphaColorPicker";
34
export { HslaColorPicker } from "./components/HslaColorPicker";
45
export { HslaStringColorPicker } from "./components/HslaStringColorPicker";
56
export { HslColorPicker } from "./components/HslColorPicker";

src/utils/convert.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ export const hexToRgba = (hex: string): RgbaColor => {
2121
r: parseInt(hex[0] + hex[0], 16),
2222
g: parseInt(hex[1] + hex[1], 16),
2323
b: parseInt(hex[2] + hex[2], 16),
24-
a: 1,
24+
a: hex.length === 4 ? round(parseInt(hex[3] + hex[3], 16) / 255, 2) : 1,
2525
};
2626
}
2727

2828
return {
2929
r: parseInt(hex.substring(0, 2), 16),
3030
g: parseInt(hex.substring(2, 4), 16),
3131
b: parseInt(hex.substring(4, 6), 16),
32-
a: 1,
32+
a: hex.length === 8 ? round(parseInt(hex.substring(6, 8), 16) / 255, 2) : 1,
3333
};
3434
};
3535

@@ -163,8 +163,9 @@ const format = (number: number) => {
163163
return hex.length < 2 ? "0" + hex : hex;
164164
};
165165

166-
export const rgbaToHex = ({ r, g, b }: RgbaColor): string => {
167-
return "#" + format(r) + format(g) + format(b);
166+
export const rgbaToHex = ({ r, g, b, a }: RgbaColor): string => {
167+
const alphaHex = a < 1 ? format(round(a * 255)) : "";
168+
return "#" + format(r) + format(g) + format(b) + alphaHex;
168169
};
169170

170171
export const rgbaToHsva = ({ r, g, b, a }: RgbaColor): HsvaColor => {

tests/components.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
HsvStringColorPicker,
1616
HsvaColorPicker,
1717
HsvaStringColorPicker,
18+
HexAlphaColorPicker,
1819
} from "../src";
1920

2021
afterEach(cleanup);
@@ -188,6 +189,19 @@ it("Changes alpha channel value after an interaction", async () => {
188189
expect(handleChange).toHaveReturnedWith({ h: 100, s: 0, l: 0, a: 1 });
189190
});
190191

192+
it("Uses #rrggbbaa format if alpha channel value is less than 1", async () => {
193+
const handleChange = jest.fn((hex) => hex);
194+
const result = render(<HexAlphaColorPicker color="#112233" onChange={handleChange} />);
195+
const alpha = result.container.querySelector(
196+
".react-colorful__alpha .react-colorful__interactive"
197+
);
198+
199+
fireEvent(alpha, new FakeMouseEvent("mousedown", { pageX: 100, pageY: 0 }));
200+
fireEvent(alpha, new FakeMouseEvent("mousemove", { pageX: 0, pageY: 0 }));
201+
202+
expect(handleChange).toHaveReturnedWith("#11223300");
203+
});
204+
191205
// Fast clicks on mobile devices
192206
// See https://github.com/omgovich/react-colorful/issues/55
193207
it("Doesn't react on mouse events after a touch interaction", () => {

0 commit comments

Comments
 (0)