js 各种颜色模式之间的转换

  1 /* RGBColor
  2 parameter: 
  3     r, g, b
  4 
  5 method:
  6     set(r, g, b: Number): this;            //rgb: 0 - 255; 第一个参数可以为 css color
  7     setFormHex(hex: Number): this;         //
  8     setFormHSV(h, s, v: Number): this;    //h:0-360; s,v:0-100; 颜色, 明度, 暗度
  9     setFormString(str: String): Number;    //str: 英文|css color; 返回的是透明度 (如果为 rgba 则返回a; 否则总是返回1)
 10 
 11     copy(v: RGBColor): this;
 12     clone(): RGBColor;
 13 
 14     getHex(): Number;
 15     getHexString(): String;
 16     getHSV(result: Object{h, s, v}): result;    //result: 默认是一个新的Object
 17     getRGBA(alpha: Number): String;             //alpha: 0 - 1; 默认 1
 18     getStyle()                                     //.getRGBA()别名
 19 
 20     stringToColor(str: String): String; //str 转为 css color; 如果str不是color格式则返回 ""
 21 
 22 */
 23 class RGBColor{
 24 
 25     constructor(r = 255, g = 255, b = 255){
 26         this.r = r;
 27         this.g = g;
 28         this.b = b;
 29 
 30     }
 31 
 32     copy(v){
 33         this.r = v.r;
 34         this.g = v.g;
 35         this.b = v.b;
 36         return this;
 37     }
 38 
 39     clone(){
 40         return new this.constructor().copy(this);
 41     }
 42 
 43     set(r, g, b){
 44         if(typeof r !== "string"){
 45             this.r = r || 255;
 46             this.g = g || 255;
 47             this.b = b || 255;
 48         }
 49 
 50         else this.setFormString(r);
 51         
 52         return this;
 53     }
 54 
 55     setFormHex(hex){
 56         hex = Math.floor( hex );
 57 
 58         this.r = hex >> 16 & 255;
 59         this.g = hex >> 8 & 255;
 60         this.b = hex & 255;
 61         return this;
 62     }
 63 
 64     setFormHSV(h, s, v){
 65         h = h >= 360 ? 0 : h;
 66         var s=s/100;
 67         var v=v/100;
 68         var h1=Math.floor(h/60) % 6;
 69         var f=h/60-h1;
 70         var p=v*(1-s);
 71         var q=v*(1-f*s);
 72         var t=v*(1-(1-f)*s);
 73         var r,g,b;
 74         switch(h1){
 75             case 0:
 76                 r=v;
 77                 g=t;
 78                 b=p;
 79                 break;
 80             case 1:
 81                 r=q;
 82                 g=v;
 83                 b=p;
 84                 break;
 85             case 2:
 86                 r=p;
 87                 g=v;
 88                 b=t;
 89                 break;
 90             case 3:
 91                 r=p;
 92                 g=q;
 93                 b=v;
 94                 break;
 95             case 4:
 96                 r=t;
 97                 g=p;
 98                 b=v;
 99                 break;
100             case 5:
101                 r=v;
102                 g=p;
103                 b=q;
104                 break;
105         }
106 
107         this.r = Math.round(r*255);
108         this.g = Math.round(g*255);
109         this.b = Math.round(b*255);
110         return this;
111     }
112 
113     setFormString(color){
114         if(typeof color !== "string") return 1;
115         var _color = this.stringToColor(color);
116         
117         if(_color[0] === "#"){
118             const len = _color.length;
119             if(len === 4){
120                 _color = _color.slice(1);
121                 this.setFormHex(parseInt("0x"+_color + "" + _color));
122             }
123             else if(len === 7) this.setFormHex(parseInt("0x"+_color.slice(1)));
124             
125         }
126 
127         else if(_color[0] === "r" && _color[1] === "g" && _color[2] === "b"){
128             const arr = [];
129             for(let k = 0, len = _color.length, v = "", is = false; k < len; k++){
130                 
131                 if(is === true){
132                     if(_color[k] === "," || _color[k] === ")"){
133                         arr.push(parseFloat(v));
134                         v = "";
135                     }
136                     else v += _color[k];
137                     
138                 }
139 
140                 else if(_color[k] === "(") is = true;
141                 
142             }
143 
144             this.set(arr[0], arr[1], arr[2]);
145             return arr[3] === undefined ? 1 : arr[3];
146         }
147         
148         return 1;
149     }
150 
151     getHex(){
152 
153         return Math.max( 0, Math.min( 255, this.r ) ) << 16 ^ Math.max( 0, Math.min( 255, this.g ) ) << 8 ^ Math.max( 0, Math.min( 255, this.b ) ) << 0;
154 
155     }
156 
157     getHexString(){
158 
159         return '#' + ( '000000' + this.getHex().toString( 16 ) ).slice( - 6 );
160 
161     }
162 
163     getHSV(result){
164         result = result || {}
165         var r=this.r/255;
166         var g=this.g/255;
167         var b=this.b/255;
168         var h,s,v;
169         var min=Math.min(r,g,b);
170         var max=v=Math.max(r,g,b);
171         var l=(min+max)/2;
172         var difference = max-min;
173         
174         if(max==min){
175             h=0;
176         }else{
177             switch(max){
178                 case r: h=(g-b)/difference+(g < b ? 6 : 0);break;
179                 case g: h=2.0+(b-r)/difference;break;
180                 case b: h=4.0+(r-g)/difference;break;
181             }
182             h=Math.round(h*60);
183         }
184         if(max==0){
185             s=0;
186         }else{
187             s=1-min/max;
188         }
189         s=Math.round(s*100);
190         v=Math.round(v*100);
191         result.h = h;
192         result.s = s;
193         result.v = v;
194         return result;
195     }
196 
197     getStyle(){
198         return this.getRGBA(1);
199     }
200 
201     getRGBA(alpha){
202         alpha = typeof alpha === 'number' ? alpha : 1;
203         return 'rgba('+this.r+','+this.g+','+this.b+','+alpha+')';
204     }
205 
206     stringToColor(str){
207         var _color = "";
208         for(let k = 0, len = str.length; k < len; k++){
209             if(str[k] === " ") continue;
210             _color += str[k];
211         }
212         
213         if(_color[0] === "#" || (_color[0] === "r" && _color[1] === "g" && _color[2] === "b")) return _color;
214         else{
215             for(let k = 0, len = ColorRefTable.length; k < len; k++){
216                 str = ColorRefTable[k];
217                 if(str[0] === _color) return str[1];
218             }
219         }
220 
221         return "";
222     }
223 
224 }
225 
226 Object.defineProperties(RGBColor.prototype, {
227 
228     isRGBColor: {
229         configurable: false,
230         enumerable: false,
231         writable: false,
232         value: true,
233     }
234 
235 });

 

posted @ 2022-05-28 17:22  鸡儿er  阅读(116)  评论(0编辑  收藏  举报