1/** 2 * color-utils for jQuery 3 * 4 * Copyright (c) 2008 Yoshiomi KURISU 5 * Licensed under the MIT (MIT-LICENSE.txt) licenses. 6 * 7 */ 8(function($){ 9 10 $.isHexadecimalColor = function(color){ 11 if(color && color.match 12 && ( color.match(/^#[0-9a-fA-F]{3}$/) || color.match(/^#[0-9a-fA-F]{6}$/)) ) return true; 13 else return false; 14 } 15 16 $.isRGBColor = function(color){ 17 if(color && color.match && color.match(/^rgb\(([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]),[ ]{0,1}([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]),[ ]{0,1}([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\)$/) ) return true; 18 else return false; 19 } 20 21 $.isColor = function(color){ 22 var $ = jQuery; 23 if($.isHexadecimalColor(color) || $.isRGBColor(color)) return true; 24 else return false; 25 } 26 27 $.RGB = function(color){ 28 var $ = jQuery; 29 if(typeof color == 'string'){ 30 if($.isRGBColor($.fmtColor(color,'rgb'))){ 31 return [parseInt(RegExp.$1),parseInt(RegExp.$2),parseInt(RegExp.$3)]; 32 } 33 return []; 34 }else if(color instanceof Array && color.length == 3 35 && isFinite(color[0]-0) && isFinite(color[1]-0) && isFinite(color[2]-0) 36 && ( 0 <= color[0]-0 && color[0] -0 <= 255) 37 && ( 0 <= color[1]-0 && color[1] -0 <= 255) 38 && ( 0 <= color[2]-0 && color[2] -0 <= 255)){ 39 return 'rgb('+color[0]+', '+color[1] + ', ' + color[2]+')'; 40 } 41 return color; 42 } 43 44 $.colorVectorValue = function(color){ 45 var $ = jQuery; 46 var c = $.RGB(color); 47 if(c.length == 3){ 48 var r = c[0],g = c[1],b = c[2]; 49 return Math.sqrt( (r*r)+(g*g)+(b*b) ).toFixed(2); 50 } 51 return 0; 52 } 53 54 $.colorDecimalValue = function(color){ 55 var $ = jQuery; 56 if($.isColor(color)){ 57 color = $.fmtColor(color,'hexadecimal'); 58 return parseInt(color.substr(1,6),16); 59 } 60 return 0; 61 } 62 63 64 $.fmtColor = function(color,fmt){ 65 var $ = jQuery; 66 if($.isColor(color) == false) return color; 67 var pad = function(str){ 68 if(str.length < 2){ 69 for(var i = 0,len = 2 - str.length ; i<len ; i++){ 70 str = '0'+str; 71 } 72 } 73 return str; 74 } 75 if(color.match(/^#[0-9a-fA-F]{3}$/)){ 76 var r = color.substr(1,1); 77 var g = color.substr(2,1); 78 var b = color.substr(3,1); 79 color = '#' + r + r + g + g + b + b; 80 } 81 82 fmt = (fmt)?fmt:(function(c){ 83 if($.isRGBColor(c)) return 'hexadecimal'; 84 else if($.isHexadecimalColor(c)) return 'rgb'; 85 else return ''; 86 })(color); 87 if (fmt == 'rgb' && $.isHexadecimalColor(color)) { 88 var r = parseInt(color.substr(1,2),16); 89 var g = parseInt(color.substr(3,2),16); 90 var b = parseInt(color.substr(5,2),16); 91 color = $.RGB([r,g,b]); 92 }else if (fmt == 'hexadecimal') { 93 var c = $.RGB(color); 94 if(c.length == 3){ 95 var r = pad(c[0].toString(16)),g = pad(c[1].toString(16)),b= pad(c[2].toString(16)); 96 color = '#' + r + g + b; 97 } 98 } 99 return color; 100 } 101 102 $.modColor = function(color,type,value,rotate){ 103 var gb = function(color,type,value,rotate){ 104 if((type=='r'|| type=='g' || type=='b') && isFinite(value)) { 105 var $ = jQuery; 106 tmp = $.fmtColor(color,'rgb'); 107 var gc = function(original_value,value,rotate){ 108 var v = parseInt(original_value) + (value -0); 109 if(rotate){ 110 v = (v > 255)?v-255:v; 111 v = (v < 0)?v+255:v; 112 }else{ 113 v = (v > 255)?255:v; 114 v = (v < 0)?0:v; 115 } 116 return v; 117 } 118 if($.isRGBColor(tmp)){ 119 var r = parseInt(RegExp.$1); 120 var g = parseInt(RegExp.$2); 121 var b = parseInt(RegExp.$3); 122 if(type=='r'){ 123 r = gc(RegExp.$1,value,rotate); 124 }else if(type=='g'){ 125 g = gc(RegExp.$2,value,rotate); 126 }else if(type=='b'){ 127 b = gc(RegExp.$3,value,rotate); 128 } 129 color = $.RGB([r,g,b]); 130 } 131 } 132 return color; 133 } 134 if(type instanceof Array && type.length == 3){ 135 type = {'r':type[0],'g':type[1],'b':type[2]}; 136 } 137 if(type instanceof Object){ 138 rotate = value; 139 var params = ['r','g','b']; 140 for(var i = 0,len = params.length ; i<len ; i++){ 141 var v = type[params[i]]; 142 if( v && isFinite(v)){ 143 color = gb(color,params[i],v,rotate); 144 } 145 } 146 return color; 147 }else if(typeof type == 'string' && isFinite(value)){ 148 return gb(color,type,value,rotate); 149 } 150 } 151 152 $.averageColor = function(colors){ 153 var $ = jQuery; 154 var color = [0,0,0]; 155 var cnt = 0; 156 for(var i =0,len=colors.length;i<len;i++){ 157 var c = $.RGB(colors[i]); 158 if(c.length == 3){ 159 color[0] = color[0] + c[0]; 160 color[1] = color[1] + c[1]; 161 color[2] = color[2] + c[2]; 162 cnt ++; 163 } 164 } 165 return $.RGB([Math.round(color[0] / cnt),Math.round(color[1] / cnt),Math.round(color[2] / cnt)]
165); 166 } 167 168 $.colorize = function(p){ 169 var $ = jQuery; 170 if(typeof p == 'string'){ 171 var t = '#' + p.replace(/o/gi,'0').replace(/[li]/gi,'1').replace(/z/gi,'2').replace(/b/gi,'6').replace(/\?/gi,'7').replace(/q/gi,'9'); 172 if($.isColor(t)) return t; 173 }else if(p instanceof Object){ 174 if(isFinite(p.min) && isFinite(p.max) && isFinite(p.value)){ 175 var total = p.max - p.min; 176 var c = parseInt(p.value * 1677216 / total).toString(16); 177 for(var i = 0,len = 6-c.length;i < len;i++){ 178 c = '0'+c; 179 } 180 return '#'+c; 181 } 182 } 183 return '#000000'; 184 } 185 186 $.fn.modColor = function(prop,type,value,rotate){ 187 var $ = jQuery; 188 var v = $(this).css(prop); 189 $(this).css(prop,$.modColor(v,type,value,rotate)); 190 return this; 191 } 192 193 $.fn.averageColor = function(prop){ 194 prop = (prop)?prop:'backgroundColor'; 195 var $ = jQuery; 196 var tmp = []; 197 for(var i = 0,len = this.length;i<len;i++){ 198 var c = this.eq(i).css(prop); 199 if($.isColor(c)){ 200 tmp.push(c); 201 } 202 } 203 return $.averageColor(tmp); 204 } 205 206 $.fn.sortByColor = function(prop){ 207 var $ = jQuery; 208 prop = (prop)?prop:'backgroundColor'; 209 210 var fn = function(a,b,prop){ 211 var t = $.colorDecimalValue($(a).css(prop)) - $.colorDecimalValue($(b).css(prop)); 212 if(t == 0) return $.colorVectorValue($(a).css(prop)) - $.colorVectorValue($(b).css(prop)); 213 else return t; 214 } 215 function q(jq, h, t,prop) { 216 var pivot = jq[parseInt( h + (t - h)/2 )]; 217 var i = h - 1; 218 var j = t + 1; 219 while (1){ 220 while (fn(jq[++i], pivot,prop) < 0); 221 while (fn(jq[--j], pivot,prop) > 0); 222 if (i >= j) break; 223 var tmp = jq[i]; 224 jq[i] = jq[j]; 225 jq[j] = tmp; 226 } 227 if (h < i - 1) q(jq, h, i - 1,prop); 228 if (j + 1 < t) q(jq, j + 1, t,prop); 229 return jq; 230 } 231 return q(this, 0, this.length - 1,prop); 232 233 } 234 235 236})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.