function transferRgbToStr(color) { if (typeof color !== 'string' && !(color instanceof String) || !~color.indexOf('rgb')) return console.error("请输入rgb形式的颜色值"); color = color.replace(/\s+/g, ''); var index = color.indexOf('(') + 1; // Note: String the slice method, the time slice method parameter is negative, that is, the reciprocal of the time // the substring method parameter is negative, that is all 0 var Colors = Color . Slice ( index , - . 1 ). Split ( ',' .) Slice ( 0 , . 3 ); for ( var I = 0 ; I < Colors . length ; I ++) { IF ( the parseInt ( Colors [ I ], 10 )> 255 ||the parseInt ( Colors [ I ], 10 ) < 0 ) return Console . error ( " color values range between 0 and 255 for the input value! ") Colors [ I ] = the parseInt ( Colors [ I ], 10 ). toString ( 16 ); IF ( Colors [ I .] length === . 1 ) { Colors [ I ] = " 0" + colors[i] } } return colors.join(""); }
transferRgbToStr incoming value is rgb (0,0,0)