Skip to content

Commit 79deb59

Browse files
authored
Merge pull request #13 from odedglas/simplify-stories
2 parents 3b91c79 + 85eea62 commit 79deb59

File tree

3 files changed

+44
-63
lines changed

3 files changed

+44
-63
lines changed

README.md

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ const WrappedColorPicker = ({ onSelect, ...rest }) => (
2727

2828
const App = () => {
2929
const [palette, setPalette] = useState([
30-
{ offset: '0.00', color: '#eef10b' },
31-
{ offset: '0.49', color: '#d78025' },
32-
{ offset: '1.00', color: '#7e20cf' }
30+
{ offset: '0.00', color: 'rgb(238, 241, 11)' },
31+
{ offset: '0.49', color: 'rgb(215, 128, 37)' },
32+
{ offset: '1.00', color: 'rgb(126, 32, 207)' }
3333
]);
3434

3535
return (
@@ -50,18 +50,19 @@ const App = () => {
5050
| Name | Type | Default Value | Required? | Description
5151
|-|-|-|-|-
5252
| `palette` | `PaletteColor[]` | `undefined` | Yes | The gradient pickers color palette, Each palette color struct is described below
53-
| `onPaletteChange` | `Function` | `undefined` | Yes | The function to trigger upon palette change (Can be neither from stop drag or color select).
53+
| `onPaletteChange` | `Function` | `undefined` | Yes | The function to trigger upon palette change (Can be either from stop drag or color select).
5454
| `paletteHeight` | `Number` | `32` | No | The stops palette display area height
5555
| `width` | `Number` | `400` | No | Determines the width of the gradient picker
5656
| `stopRemovalDrop` | `Number` | `50` | No | Sets the Y stop drop removal offset, If the user will drag the color stop further than specified, Color will be removed
5757
| `maxStops` | `Number` | `5` | No | The max gradient picker palette length can have
5858
| `minStops` | `Number` | `2` | No | The min gradient picker palette length can have
5959

60-
PaletteColor = shape({
61-
color: string.isRequired,
62-
offset: string.isRequired,
63-
opacity: number,
64-
});
60+
|> Palette Color
61+
| Name | Type | Default Value | Required? | Description
62+
|-|-|-|-|-
63+
| `color` | `String` | `` | Yes | The stop color, can be either hex of rgb format.
64+
| `offset`| `Number` | `` | `Yes` | The stop color offset in percent.
65+
| `opacity`| `Number` | `1` | `No` | The stop color opacity.
6566

6667
## Angle Picke Usage
6768
<img width="200" alt="gradient_preview" src="/assets/ap.png"> <br/>
@@ -97,32 +98,24 @@ import { SketchPicker } from 'react-color';
9798
import { GradientPickerPopover } from 'react-linear-gradient-picker';
9899
import './index.css';
99100

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-
}
101+
const rgbToRgba = (rgb, a = 1) => rgb
102+
.replace('rgb(', 'rgba(')
103+
.replace(')', `, ${a})`)
118104

119-
const WrappedSketchPicker = ({ onSelect, ...rest }) => (
120-
<SketchPicker {...rest}
121-
color={addOpacityToHex(rest.color, rest.opacity)}
122-
onChange={c => {
123-
onSelect(c.hex, c.rgb.a);
124-
}}/>
125-
);
105+
const WrapperPropTypes = {
106+
onSelect: PropTypes.func
107+
};
108+
109+
const WrappedSketchPicker = ({ onSelect, ...rest }) => {
110+
return (
111+
<SketchPicker {...rest}
112+
color={rgbToRgba(rest.color, rest.opacity)}
113+
onChange={c => {
114+
const { r, g, b, a } = c.rgb;
115+
onSelect(`rgb(${r}, ${g}, ${b})`, a);
116+
}}/>
117+
);
118+
}
126119

127120
const initialPallet = [
128121
{ offset: '0.00', color: 'rgb(238, 241, 11)' },

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-linear-gradient-picker",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"description": "React linear gradient picker",
55
"main": "dist/index.js",
66
"scripts": {

stories/GradientPicker/index.js

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

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

9-
/**
10-
* (c) https://stackoverflow.com/questions/21646738/convert-hex-to-rgba
11-
*/
12-
function addOpacityToHex(hex, a = 1) {
13-
let 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-
}
9+
const rgbToRgba = (rgb, a = 1) => rgb
10+
.replace('rgb(', 'rgba(')
11+
.replace(')', `, ${a})`)
2712

2813
const WrapperPropTypes = {
2914
onSelect: PropTypes.func
3015
};
3116

32-
const WrappedSketchPicker = ({ onSelect, ...rest }) => (
33-
<SketchPicker {...rest}
34-
color={addOpacityToHex(rest.color, rest.opacity)}
35-
onChange={c => {
36-
onSelect(c.hex, c.rgb.a);
37-
}}/>
38-
);
17+
const WrappedSketchPicker = ({ onSelect, ...rest }) => {
18+
return (
19+
<SketchPicker {...rest}
20+
color={rgbToRgba(rest.color, rest.opacity)}
21+
onChange={c => {
22+
const { r, g, b, a } = c.rgb;
23+
onSelect(`rgb(${r}, ${g}, ${b})`, a);
24+
}}/>
25+
);
26+
}
3927

4028
WrappedSketchPicker.propTypes = WrapperPropTypes;
4129

@@ -49,9 +37,9 @@ WrappedColorPicker.propTypes = WrapperPropTypes;
4937

5038
const SketchPickerStory = () => (
5139
<UseCase palette={[
52-
{ offset: '0.00', color: '#eef10b' },
53-
{ offset: '0.49', color: '#d78025' },
54-
{ offset: '1.00', color: '#7e20cf' }
40+
{ offset: '0.00', color: 'rgb(238, 241, 11)' },
41+
{ offset: '0.49', color: 'rgb(215, 128, 37)' },
42+
{ offset: '1.00', color: 'rgb(126, 32, 207)' }
5543
]} link={'https://github.com/react-component/color-picker'} title={'rc-color-picker'} ColorPicker={WrappedSketchPicker}/>
5644
);
5745

0 commit comments

Comments
 (0)