SelectColorView.js 10.3 KB
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 SelectColorView extends Component {

    constructor(props) {
        super(props);
        this.state = {
            selectColor: defaultColor,// 选择的颜色
            selectRValue: 0,// R值
            selectGValue: 0,// G值
            selectBValue: 0,// B值
            showSelectColor: false,
        };
        this.onEnter = this.props.onEnter;
        this.onCancel = this.props.onCancel;
        this.selectColorList = ['#010101', '#3664FF', '#67C858',
            '#F6CC2A', '#F34434'];
    }

    resetInitColor(selectColor) {
        if (selectColor && selectColor.length > 0) {
            let rgbColorObj = colorsys.parseCss(selectColor);
            this.setState({
                showSelectColor:true,
                selectColor: selectColor,
                selectRValue: rgbColorObj.r,
                selectGValue: rgbColorObj.g,// G值
                selectBValue: rgbColorObj.b,// B值
            })
        }
    }

    /**
     * 颜色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.setState({
                        showSelectColor:false,
                    });
                    if (this.onEnter) {
                        this.onEnter(value)
                    }
                }}
            >
                <View style={{borderRadius: 20, width: 30, height: 30, backgroundColor: value}}/>
            </TouchableOpacity>
        );
    }

    render() {
        if (this.state.showSelectColor) {
            return (
                <View style={styles.contentBg}>
                    {/** 标题栏 */}
                    <View style={styles.titleBar}>
                        <TouchableOpacity
                            style={{
                                justifyContent: "center",
                                alignItems: "center",
                                paddingVertical: 3,
                                paddingHorizontal: 15,
                            }}
                            activeOpacity={0.8}
                            onPress={() => {
                                this.setState({
                                    showSelectColor:false,
                                });
                                if (this.onCancel) {
                                    this.onCancel();
                                }
                            }}
                        >
                            <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={() => {
                                // 完成颜色选择
                                if (this.onEnter) {
                                    this.setState({
                                        showSelectColor:false,
                                    });
                                    let selectColor = colorsys.rgb2Hex(this.state.selectRValue, this.state.selectGValue, this.state.selectBValue);
                                    this.onEnter(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
                                    });
                                }}
                            />
                            <View style={{marginLeft: 6,width: 60, height: 36, justifyContent: 'center', alignItems: 'center', borderWidth: 1,
                                borderColor: global.homeColor, borderRadius: 4}}>
                                <Text style={{fontSize: 16, color: 'black',  textAlign: 'center'}}>{this.state.selectRValue+''}</Text>
                            </View>
                        </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
                                    });
                                }}
                            />
                            <View style={{marginLeft: 6,width: 60, height: 36, justifyContent: 'center', alignItems: 'center', borderWidth: 1,
                                borderColor: global.homeColor, borderRadius: 4}}>
                                <Text style={{fontSize: 16, color: 'black',  textAlign: 'center'}}>{this.state.selectGValue+''}</Text>
                            </View>
                        </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
                                    });
                                }}
                            />
                            <View style={{marginLeft: 6,width: 60, height: 36, justifyContent: 'center', alignItems: 'center', borderWidth: 1,
                                borderColor: global.homeColor, borderRadius: 4}}>
                                <Text style={{fontSize: 16, color: 'black',  textAlign: 'center'}}>{this.state.selectBValue+''}</Text>
                            </View>
                        </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>
            );
        } else {
            return null;
        }
    }
}

const styles = StyleSheet.create({
    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"
    },
});