Skip to content

Commit a8a4770

Browse files
authored
Merge pull request #56 from cuongdevjs/master
improve:
2 parents 520b11c + 445ed80 commit a8a4770

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/__tests__/input-color.js

Lines changed: 6 additions & 2 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 } from '../utils';
4+
import { parseColor, hex2alpha, rgbaToHex } from '../utils';
55

66
test('render', () => {
77
expect(() =>
@@ -29,7 +29,7 @@ test('parseColor', () => {
2929
s: 76,
3030
v: 86,
3131
a: 20,
32-
hex: '#3498db',
32+
hex: '#3498db32',
3333
rgba: 'rgba(52,152,219,0.2)',
3434
});
3535
});
@@ -40,3 +40,7 @@ test('hex2alpha', () => {
4040
expect(hex2alpha('38')).toEqual(22);
4141
expect(hex2alpha('00')).toEqual(0);
4242
});
43+
44+
test('rgbaToHex', () => {
45+
expect(rgbaToHex('rgba(255, 255, 255,0.5))')).toEqual(`#ffffff80`);
46+
});

src/color-picker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
rgba,
1111
hsv2rgb,
1212
} from '@swiftcarrot/color-fns';
13+
import { rgbaToHex } from './utils';
1314

1415
const KEY_ENTER = 13;
1516

@@ -35,7 +36,8 @@ const ColorPicker = ({ color, onChange }) => {
3536
}
3637

3738
function changeAlpha(a) {
38-
changeColor({ ...color, a });
39+
const hex = rgbaToHex(`rgba(${r}, ${g}, ${b}, ${a / 100})`);
40+
changeColor({ ...color, a, hex });
3941
}
4042

4143
function changeHex(hex) {

src/utils.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function hex2alpha(aa) {
66

77
export function parseColor(hexColor) {
88
hexColor = hexColor.toLowerCase();
9-
const hex = hexColor.substr(0, 7);
9+
const hex = hexColor;
1010
const rgb = hex2rgb(hex);
1111
const { r, g, b } = rgb;
1212
const hsv = rgb2hsv(r, g, b);
@@ -15,6 +15,34 @@ export function parseColor(hexColor) {
1515
return { ...hsv, r, g, b, a, hex, rgba: rgba(r, g, b, a) };
1616
}
1717

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('');
44+
}
45+
1846
export {
1947
rgb2hsv,
2048
hsv2hex,

0 commit comments

Comments
 (0)