Skip to content

Commit 42eb8cb

Browse files
committed
Error when opacity = 0.
Sometime input is rgb instead of hex
1 parent d150d9c commit 42eb8cb

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,24 @@ import { SketchPicker } from 'react-color';
9797
import { GradientPickerPopover } from 'react-linear-gradient-picker';
9898
import './index.css';
9999

100-
const addOpacityToHex = (color, opacity = 1) => {
101-
if (opacity === 1 || color.length > 9) {
102-
return color;
103-
}
104-
105-
return color + Math.floor(opacity * 255).toString(16);
106-
};
100+
/**
101+
* (c) https://stackoverflow.com/questions/21646738/convert-hex-to-rgba
102+
*/
103+
function addOpacityToHex(hex, a = 1){
104+
var c;
105+
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
106+
c= hex.substring(1).split('');
107+
if(c.length== 3){
108+
c= [c[0], c[0], c[1], c[1], c[2], c[2]];
109+
}
110+
c= '0x'+c.join('');
111+
return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+','+a+')';
112+
}
113+
if (/rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/.test(hex)) { /** RGB color */
114+
return hex.replace('rgb', 'rgba').replace(')', `, ${a})`);
115+
}
116+
throw new Error('Bad Hex');
117+
}
107118

108119
const WrappedSketchPicker = ({ onSelect, ...rest }) => (
109120
<SketchPicker {...rest}

stories/GradientPicker/index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ import UseCase from './UseCase';
66

77
import 'rc-color-picker/assets/index.css';
88

9-
const addOpacityToHex = (color, opacity = 1) => {
10-
if (opacity === 1 || color.length > 9) {
11-
return color;
12-
}
13-
14-
return color + Math.floor(opacity * 255).toString(16);
15-
};
9+
/**
10+
* (c) https://stackoverflow.com/questions/21646738/convert-hex-to-rgba
11+
*/
12+
function addOpacityToHex(hex, a = 1){
13+
var c;
14+
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
15+
c= hex.substring(1).split('');
16+
if(c.length== 3){
17+
c= [c[0], c[0], c[1], c[1], c[2], c[2]];
18+
}
19+
c= '0x'+c.join('');
20+
return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+','+a+')';
21+
}
22+
if (/rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/.test(hex)) { /** RGB color */
23+
return hex.replace('rgb', 'rgba').replace(')', `, ${a})`);
24+
}
25+
throw new Error('Bad Hex');
26+
}
1627

1728
const WrapperPropTypes = {
1829
onSelect: PropTypes.func

stories/Popover/index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ import { SketchPicker } from 'react-color';
44
import GradientPickerPopover from '../../src/components/GradientPickerPopover';
55
import './index.css';
66

7-
const addOpacityToHex = (color, opacity = 1) => {
8-
if (opacity === 1 || color.length > 9) {
9-
return color;
10-
}
11-
12-
return color + Math.floor(opacity * 255).toString(16);
13-
};
7+
/**
8+
* (c) https://stackoverflow.com/questions/21646738/convert-hex-to-rgba
9+
*/
10+
function addOpacityToHex(hex, a = 1){
11+
var c;
12+
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
13+
c= hex.substring(1).split('');
14+
if(c.length== 3){
15+
c= [c[0], c[0], c[1], c[1], c[2], c[2]];
16+
}
17+
c= '0x'+c.join('');
18+
return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+','+a+')';
19+
}
20+
if (/rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/.test(hex)) { /** RGB color */
21+
return hex.replace('rgb', 'rgba').replace(')', `, ${a})`);
22+
}
23+
throw new Error('Bad Hex');
24+
}
1425

1526
const WrappedSketchPicker = ({ onSelect, ...rest }) => (
1627
<SketchPicker {...rest}

0 commit comments

Comments
 (0)