SelectColorModal.js
10.5 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
TouchableOpacity,
Image,
Modal, ScrollView, Slider
} from 'react-native';
import {width} from "../utils/getSize";
const defaultColor = '#FF8D1A';
const colorsys = require('colorsys');
export default class SelectColorModal extends Component {
constructor(props) {
super(props);
this.state = {
showSelectColorModal: false,
selectColor: defaultColor,// 选择的颜色
selectRValue: 0,// R值
selectGValue: 0,// G值
selectBValue: 0,// B值
};
this.onCallback = this.props.onCallback;
this.selectColorList = ['#010101', '#3664FF', '#67C858',
'#F6CC2A', '#F34434'];
}
resetInitColor(selectColor) {
if (selectColor && selectColor.length > 0) {
let rgbColorObj = colorsys.parseCss(selectColor);
this.setState({
selectColor: selectColor,
selectRValue: rgbColorObj.r,
selectGValue: rgbColorObj.g,// G值
selectBValue: rgbColorObj.b,// B值
})
}
}
showSelectColorModal(selectColor) {
this.setState({
showSelectColorModal: true
})
this.resetInitColor(selectColor);
}
closeModal() {
this.setState({
showSelectColorModal: false
})
}
/**
* 颜色item
* @param value
* @param index
*/
renderSelectColorItem(value, index) {
return (
<TouchableOpacity
key={index}
style={{width: '20%', paddingTop: 20, justifyContent: 'center', alignItems: 'center'}}
activeOpacity={0.8}
onPress={()=>{
this.closeModal();
if (this.onCallback) {
this.onCallback(value)
}
}}
>
<View style={{borderRadius: 20, width: 30, height: 30, backgroundColor: value}}/>
</TouchableOpacity>
);
}
render() {
return (
<Modal animationType={"fade"} transparent={true} visible={this.state.showSelectColorModal} onRequestClose={() => {}}>
{/** 目录弹窗 */}
<View style={{flex: 1}}>
<TouchableOpacity style={styles.modalBg} activeOpacity={1} onPress={() => this.closeModal()}/>
{/** 实体内容 */}
<View style={styles.contentBg}>
{/** 标题栏 */}
<View style={styles.titleBar}>
<TouchableOpacity
style={{
justifyContent: "center",
alignItems: "center",
paddingVertical: 3,
paddingHorizontal: 15,
}}
activeOpacity={0.8}
onPress={() => this.closeModal()}
>
<Text style={{fontSize: 16, color: 'rgba(128, 128, 128, 1)'}}>取消</Text>
</TouchableOpacity>
<Text style={styles.modelTitle}>颜色</Text>
<TouchableOpacity
style={{
justifyContent: "center",
alignItems: "center",
paddingVertical: 3,
paddingHorizontal: 15,
}}
activeOpacity={0.8}
onPress={() => {
this.closeModal();
// 完成颜色选择
if (this.onCallback) {
let selectColor = colorsys.rgb2Hex(this.state.selectRValue, this.state.selectGValue, this.state.selectBValue);
this.onCallback(selectColor);
}
}}
>
<Text style={{fontSize: 16, color: global.homeColor}}>完成</Text>
</TouchableOpacity>
</View>
{/** 内容 */}
<ScrollView style={{flex: 1}} horizontal={false} showsVerticalScrollIndicator={false}>
<View style={{backgroundColor: this.state.selectColor, width: 200, height: 120,alignSelf: 'center', borderRadius:4, marginTop: 15}}/>
<View style={{flexDirection: 'row', marginHorizontal: 20, marginTop: 10, alignItems: 'center'}}>
<Text style={{fontSize: 16, color: 'black', marginRight: 6}}>R</Text>
<Slider
style={{flex: 1}}
minimumValue={0}
maximumValue={255}
step={1}
value={this.state.selectRValue}
onValueChange={(value) => {
let _color = 'rgb('+value+','+this.state.selectGValue+','+this.state.selectBValue+')';
this.setState({
selectColor: _color,
selectRValue: value
});
}}
/>
<Text style={{marginLeft: 6,fontSize: 16, color: 'black', width: 60, height: 36, textAlign: 'center', textAlignVertical: 'center',
borderWidth: 1, borderColor: global.homeColor}}>{this.state.selectRValue+''}</Text>
</View>
{/** G值选择 */}
<View style={{flexDirection: 'row', marginHorizontal: 20, marginTop: 10, alignItems: 'center'}}>
<Text style={{fontSize: 16, color: 'black', marginRight: 6}}>G</Text>
<Slider
style={{flex: 1}}
minimumValue={0}
maximumValue={255}
step={1}
value={this.state.selectGValue}
onValueChange={(value) => {
let _color = 'rgb('+this.state.selectRValue+','+value+','+this.state.selectBValue+')';
this.setState({
selectColor: _color,
selectGValue: value
});
}}
/>
<Text style={{marginLeft: 6,fontSize: 16, color: 'black', width: 60, height: 36, textAlign: 'center', textAlignVertical: 'center',
borderWidth: 1, borderColor: global.homeColor}}>{this.state.selectGValue+''}</Text>
</View>
{/** B值选择 */}
<View style={{flexDirection: 'row', marginHorizontal: 20, marginTop: 10, alignItems: 'center'}}>
<Text style={{fontSize: 16, color: 'black', marginRight: 6}}>G</Text>
<Slider
style={{flex: 1}}
minimumValue={0}
maximumValue={255}
step={1}
value={this.state.selectBValue}
onValueChange={(value) => {
let _color = 'rgb('+this.state.selectRValue+','+this.state.selectGValue+','+value+')';
this.setState({
selectColor: _color,
selectBValue: value
});
}}
/>
<Text style={{marginLeft: 6,fontSize: 16, color: 'black', width: 60, height: 36, textAlign: 'center', textAlignVertical: 'center',
borderWidth: 1, borderColor: global.homeColor}}>{this.state.selectBValue+''}</Text>
</View>
<View style={{marginVertical: 15, width: width, height: 1, backgroundColor: 'rgba(0,0,0,0.4)'}}/>
{/** 选取常用颜色 */}
<Text style={{marginLeft: 20, fontSize: 16, color: 'black'}}>常用颜色</Text>
<View style={{flexDirection: 'row', justifyContent: 'flex-start', flexWrap: 'wrap', alignItems: 'flex-start',
paddingHorizontal: 20, width: width}}>
{this.selectColorList && this.selectColorList.length > 0 && this.selectColorList.map(
((value, index) => this.renderSelectColorItem(value, index)))}
</View>
</ScrollView>
</View>
</View>
</Modal>
);
}
}
const styles = StyleSheet.create({
modalBg: {
backgroundColor: 'rgba(0,0,0,.5)',
width: '100%',
height: '100%',
display: 'flex',
position: 'absolute'
},
contentBg: {
width: '100%',
flex: 1,
marginTop: 122,
backgroundColor: "rgba(255, 255, 255, 1)",
borderTopLeftRadius: 16,
borderTopRightRadius: 16,
},
titleBar: {
flexDirection: "row",
justifyContent: "flex-start",
alignItems: "center",
paddingVertical: 10,
paddingRight: 15,
borderTopLeftRadius: 16,
borderTopRightRadius: 16,
backgroundColor: 'rgba(235, 235, 235, 1)'
},
modelTitle: {
flex: 1,
color: "rgba(0, 0, 0, 1)",
fontSize: 16,
textAlign: "center",
justifyContent: "center"
},
});