forked from dudyn5ky1/react-native-get-pixel-color
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (30 loc) · 909 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import {NativeModules, Platform} from 'react-native';
const rgb2hex = rgb => {
return (rgb && rgb.length === 3) ? '#' +
('0' + parseInt(rgb[0], 10).toString(16)).slice(-2) +
('0' + parseInt(rgb[1], 10).toString(16)).slice(-2) +
('0' + parseInt(rgb[2], 10).toString(16)).slice(-2) : '';
};
const pixelColor = Platform.OS === 'ios' ? NativeModules.RNPixelColor : NativeModules.GetPixelColor;
export const setImage = (path) => new Promise((resolve, reject) => {
pixelColor.init(path, (err, isSet) => {
if (err) {
return reject(err);
}
if (isSet) {
resolve('Image sucessfully set');
}
});
});
export const pickColorAt = (x, y) => new Promise((resolve, reject) => {
pixelColor.getRGB(x, y, (err, color) => {
if (err) {
return reject(err);
}
resolve(rgb2hex(color).toUpperCase());
});
});
export default {
setImage,
pickColorAt,
};