Skip to content

Commit a31936c

Browse files
committed
add rgba2hex
1 parent a8a4770 commit a31936c

File tree

3 files changed

+25
-34
lines changed

3 files changed

+25
-34
lines changed

src/__tests__/input-color.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import renderer from 'react-test-renderer';
33
import InputColor from '../';
4-
import { parseColor, hex2alpha, rgbaToHex } from '../utils';
4+
import { parseColor, hex2alpha, rgba2hex, alpha2hex } from '../utils';
55

66
test('render', () => {
77
expect(() =>
@@ -41,6 +41,13 @@ test('hex2alpha', () => {
4141
expect(hex2alpha('00')).toEqual(0);
4242
});
4343

44-
test('rgbaToHex', () => {
45-
expect(rgbaToHex('rgba(255, 255, 255,0.5))')).toEqual(`#ffffff80`);
44+
test('alpha2hex', () => {
45+
expect(alpha2hex(100)).toEqual('ff');
46+
expect(alpha2hex(97)).toEqual('f7');
47+
expect(alpha2hex(22)).toEqual('38');
48+
expect(alpha2hex(0)).toEqual('00');
49+
});
50+
51+
test('rgba2hex', () => {
52+
expect(rgba2hex(255, 255, 255, 50)).toEqual(`#ffffff80`);
4653
});

src/color-picker.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
rgba,
1111
hsv2rgb,
1212
} from '@swiftcarrot/color-fns';
13-
import { rgbaToHex } from './utils';
13+
import { rgba2hex } from './utils';
1414

1515
const KEY_ENTER = 13;
1616

@@ -19,7 +19,11 @@ const ColorPicker = ({ color, onChange }) => {
1919

2020
function changeColor(color) {
2121
if (onChange) {
22-
onChange({ ...color, rgba: rgba(color.r, color.g, color.b, color.a) });
22+
onChange({
23+
...color,
24+
rgba: rgba(color.r, color.g, color.b, color.a),
25+
hex: rgba2hex(color.r, color.g, color.b, color.a),
26+
});
2327
}
2428
}
2529

@@ -36,8 +40,7 @@ const ColorPicker = ({ color, onChange }) => {
3640
}
3741

3842
function changeAlpha(a) {
39-
const hex = rgbaToHex(`rgba(${r}, ${g}, ${b}, ${a / 100})`);
40-
changeColor({ ...color, a, hex });
43+
changeColor({ ...color, a });
4144
}
4245

4346
function changeHex(hex) {

src/utils.js

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { hex2rgb, rgb2hsv, rgba } from '@swiftcarrot/color-fns';
1+
import { hex2rgb, rgb2hsv, rgba, rgb2hex } from '@swiftcarrot/color-fns';
22

33
export function hex2alpha(aa) {
44
return Math.round((parseInt('0x' + aa, 16) / 255) * 100);
55
}
66

7+
export function alpha2hex(a) {
8+
return (Math.round((a / 100) * 255) + 0x10000).toString(16).substr(-2);
9+
}
10+
711
export function parseColor(hexColor) {
812
hexColor = hexColor.toLowerCase();
913
const hex = hexColor;
@@ -15,32 +19,9 @@ export function parseColor(hexColor) {
1519
return { ...hsv, r, g, b, a, hex, rgba: rgba(r, g, b, a) };
1620
}
1721

18-
export function trim(str) {
19-
return str.replace(/^\s+|\s+$/gm, '');
20-
}
21-
22-
export function rgbaToHex(rgba) {
23-
let inParts = rgba.substring(rgba.indexOf('(')).split(','),
24-
r = parseInt(trim(inParts[0].substring(1)), 10),
25-
g = parseInt(trim(inParts[1]), 10),
26-
b = parseInt(trim(inParts[2]), 10),
27-
a = parseFloat(
28-
trim(inParts[3].substring(0, inParts[3].length - 1)),
29-
).toFixed(2);
30-
let outParts = [
31-
r.toString(16),
32-
g.toString(16),
33-
b.toString(16),
34-
Math.round(a * 255)
35-
.toString(16)
36-
.substring(0, 2),
37-
];
38-
outParts.forEach(function (part, i) {
39-
if (part.length === 1) {
40-
outParts[i] = '0' + part;
41-
}
42-
});
43-
return '#' + outParts.join('');
22+
export function rgba2hex(r, g, b, a) {
23+
const hex = rgb2hex(r, g, b);
24+
return hex + alpha2hex(a);
4425
}
4526

4627
export {

0 commit comments

Comments
 (0)