Skip to content

Commit b1b8883

Browse files
authored
Merge pull request #44 from swiftcarrot/true-alpha
true alpha support fix #40
2 parents 5d70235 + f67e863 commit b1b8883

File tree

12 files changed

+7740
-1764
lines changed

12 files changed

+7740
-1764
lines changed

.storybook/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
stories: ['../stories/**/*.stories.js'],
3+
addons: ['@storybook/addon-actions', '@storybook/addon-links'],
4+
};

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## 3.0.0
11+
12+
- true alpha support with `initialValue`
13+
- remove `initialHexColor` support
14+
- set alpha 0 - 100
15+
1016
## 2.0.29
1117

1218
- fix typescript definition

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ yarn add react-input-color
2727

2828
### Usage
2929

30+
The `initialValue` property supports 6 (`#RRGGBB`) and 8 (`#RRGGBBAA`) digits hex value.
31+
3032
```javascript
3133
import React from 'react';
3234
import InputColor from 'react-input-color';
@@ -37,7 +39,7 @@ function App() {
3739
return (
3840
<div>
3941
<InputColor
40-
initialHexColor="#5e72e4"
42+
initialValue="#5e72e4"
4143
onChange={setColor}
4244
placement="right"
4345
/>

index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ export class Color {
99
b: number;
1010
a: number;
1111
hex: string;
12+
rgba: string;
1213
}
1314

1415
export interface InputColorProps {
15-
initialHexColor: string;
16+
initialValue: string;
1617
placement?: string;
1718
onChange?(color: Color): void;
1819
}
1920

20-
export default function InputColor(props: InputColorProps)
21+
export default function InputColor(props: InputColorProps);

package.json

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"build": "rm -rf dist && NODE_ENV=production rollup -c",
1111
"test": "jest --coverage",
1212
"prepublishOnly": "npm test && npm run build",
13-
"watch": "rollup -cw"
13+
"watch": "rollup -cw",
14+
"storybook": "start-storybook -p 6006",
15+
"build-storybook": "build-storybook"
1416
},
1517
"files": [
1618
"index.d.ts",
@@ -27,19 +29,25 @@
2729
"author": "Wang Zuo",
2830
"license": "MIT",
2931
"devDependencies": {
30-
"babel-jest": "^24.8.0",
31-
"babel-preset-swiftcarrot": "^1.0.0",
32-
"jest": "^24.8.0",
32+
"@babel/core": "^7.9.0",
33+
"@rollup/plugin-commonjs": "^11.0.2",
34+
"@rollup/plugin-node-resolve": "^7.1.1",
35+
"@storybook/addon-actions": "^5.3.18",
36+
"@storybook/addon-links": "^5.3.18",
37+
"@storybook/addons": "^5.3.18",
38+
"@storybook/react": "^5.3.18",
39+
"babel-jest": "^25.2.4",
40+
"babel-loader": "^8.1.0",
41+
"babel-preset-swiftcarrot": "^1.1.0",
42+
"jest": "^25.2.4",
3343
"react": "^16.8.6",
3444
"react-dom": "^16.8.6",
3545
"react-test-renderer": "^16.8.6",
36-
"rollup": "^1.17.0",
37-
"rollup-plugin-babel": "^4.3.3",
38-
"rollup-plugin-commonjs": "^10.0.1",
39-
"rollup-plugin-node-resolve": "^5.2.0"
46+
"rollup": "^2.3.2",
47+
"rollup-plugin-babel": "^4.4.0"
4048
},
4149
"dependencies": {
42-
"@babel/runtime": "^7.5.5",
50+
"@babel/runtime": "^7.9.2",
4351
"@emotion/core": "^10.0.14",
4452
"@swiftcarrot/color-fns": "^3.0.4",
4553
"@xkit/popover": "^0.1.20",

rollup.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import babel from 'rollup-plugin-babel';
2-
import commonjs from 'rollup-plugin-commonjs';
3-
import nodeResolve from 'rollup-plugin-node-resolve';
2+
import commonjs from '@rollup/plugin-commonjs';
3+
import nodeResolve from '@rollup/plugin-node-resolve';
44
import pkg from './package.json';
55

66
const input = './src/index.js';

src/__tests__/input-color.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
11
import React from 'react';
22
import renderer from 'react-test-renderer';
33
import InputColor from '../';
4+
import { parseColor, hex2alpha } from '../utils';
45

56
test('render', () => {
67
expect(() =>
7-
renderer.create(<InputColor initialHexColor="#3498db" />)
8+
renderer.create(<InputColor initialValue="#3498db" />)
89
).not.toThrow();
910
});
11+
12+
test('parseColor', () => {
13+
expect(parseColor('#3498db')).toEqual({
14+
r: 52,
15+
g: 152,
16+
b: 219,
17+
h: 204,
18+
s: 76,
19+
v: 86,
20+
a: 100,
21+
hex: '#3498db',
22+
rgba: 'rgba(52,152,219,1)'
23+
});
24+
expect(parseColor('#3498db32')).toEqual({
25+
r: 52,
26+
g: 152,
27+
b: 219,
28+
h: 204,
29+
s: 76,
30+
v: 86,
31+
a: 20,
32+
hex: '#3498db',
33+
rgba: 'rgba(52,152,219,0.2)'
34+
});
35+
});
36+
37+
test('hex2alpha', () => {
38+
expect(hex2alpha('ff')).toEqual(100);
39+
expect(hex2alpha('f7')).toEqual(97);
40+
expect(hex2alpha('38')).toEqual(22);
41+
expect(hex2alpha('00')).toEqual(0);
42+
});

src/color-picker.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import InputNumber from 'react-input-number';
55
import {
66
rgb2hsv,
77
hsv2hex,
8-
rgba2hex,
8+
rgb2hex,
99
hex2rgb,
1010
rgba,
1111
hsv2rgb
@@ -18,25 +18,24 @@ const ColorPicker = ({ color, onChange }) => {
1818

1919
function changeColor(color) {
2020
if (onChange) {
21-
onChange(color);
21+
onChange({ ...color, rgba: rgba(color.r, color.g, color.b, color.a) });
2222
}
2323
}
2424

2525
function changeHSV(h, s, v) {
2626
const { r, g, b } = hsv2rgb(h, s, v);
27-
const hex = rgba2hex(r, g, b, a);
27+
const hex = rgb2hex(r, g, b);
2828
changeColor({ ...color, h, s, v, r, g, b, hex });
2929
}
3030

3131
function changeRGB(r, g, b) {
32-
const hex = rgba2hex(r, g, b, a);
32+
const hex = rgb2hex(r, g, b);
3333
const { h, s, v } = rgb2hsv(r, g, b);
3434
changeColor({ ...color, r, g, b, h, s, v, hex });
3535
}
3636

3737
function changeAlpha(a) {
38-
const hex = rgba2hex(r, g, b, a);
39-
changeColor({ ...color, a, hex });
38+
changeColor({ ...color, a });
4039
}
4140

4241
function changeHex(hex) {
@@ -190,7 +189,12 @@ const ColorPicker = ({ color, onChange }) => {
190189
</div>
191190

192191
<div css={styles.input}>
193-
<InputNumber value={a} onChange={a => changeAlpha(a)} />
192+
<InputNumber
193+
min={0}
194+
max={100}
195+
value={a}
196+
onChange={a => changeAlpha(a)}
197+
/>
194198
<div>A</div>
195199
</div>
196200
</div>
@@ -199,7 +203,7 @@ const ColorPicker = ({ color, onChange }) => {
199203
};
200204

201205
ColorPicker.defaultProps = {
202-
initialHexColor: '#5e72e4'
206+
initialValue: '#5e72e4'
203207
};
204208

205209
const styles = {

src/input-color.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import Popover from '@xkit/popover';
55
import ColorPicker from './color-picker';
66
import { parseColor } from './utils';
77

8-
const InputColor = ({ initialHexColor, onChange, placement, ...props }) => {
9-
const [color, setColor] = useState(parseColor(initialHexColor));
8+
const InputColor = ({ initialValue, onChange, placement, ...props }) => {
9+
const [color, setColor] = useState(parseColor(initialValue));
1010

1111
useEffect(() => {
12-
changeColor(parseColor(initialHexColor));
13-
}, [initialHexColor]);
12+
changeColor(parseColor(initialValue));
13+
}, [initialValue]);
1414

1515
function changeColor(color) {
1616
if (onChange) {
@@ -46,7 +46,7 @@ const InputColor = ({ initialHexColor, onChange, placement, ...props }) => {
4646
height: 100%;
4747
cursor: pointer;
4848
`}
49-
style={{ backgroundColor: color.hex }}
49+
style={{ backgroundColor: color.rgba }}
5050
/>
5151
</span>
5252
</Popover>

src/utils.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
import { hex2rgb, rgb2hsv } from '@swiftcarrot/color-fns';
1+
import { hex2rgb, rgb2hsv, rgba } from '@swiftcarrot/color-fns';
2+
3+
export function hex2alpha(aa) {
4+
return Math.round((parseInt('0x' + aa, 16) / 255) * 100);
5+
}
26

37
export function parseColor(hexColor) {
4-
const rgb = hex2rgb(hexColor);
8+
hexColor = hexColor.toLowerCase();
9+
const hex = hexColor.substr(0, 7);
10+
const rgb = hex2rgb(hex);
511
const { r, g, b } = rgb;
612
const hsv = rgb2hsv(r, g, b);
13+
const a = hexColor.length > 7 ? hex2alpha(hexColor.substr(7)) : 100;
714

8-
return { ...hsv, r, g, b, a: 100, hex: hexColor };
15+
return { ...hsv, r, g, b, a, hex, rgba: rgba(r, g, b, a) };
916
}
1017

1118
export {
1219
rgb2hsv,
1320
hsv2hex,
14-
rgba2hex,
1521
hex2rgb,
1622
rgba,
1723
hsv2rgb

0 commit comments

Comments
 (0)