color.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. function hsvtoRgb(h, s, v) {
  2. var r;
  3. var g;
  4. var b;
  5. var i;
  6. var f;
  7. var p;
  8. var q;
  9. var t;
  10. i = Math.floor(h * 6);
  11. f = h * 6 - i;
  12. p = v * (1 - s);
  13. q = v * (1 - f * s);
  14. t = v * (1 - (1 - f) * s);
  15. switch (i % 6) {
  16. case 0:
  17. r = v;
  18. g = t;
  19. b = p;
  20. break;
  21. case 1:
  22. r = q;
  23. g = v;
  24. b = p;
  25. break;
  26. case 2:
  27. r = p;
  28. g = v;
  29. b = t;
  30. break;
  31. case 3:
  32. r = p;
  33. g = q;
  34. b = v;
  35. break;
  36. case 4:
  37. r = t;
  38. g = p;
  39. b = v;
  40. break;
  41. case 5:
  42. r = v;
  43. g = p;
  44. b = q;
  45. break;
  46. default:
  47. break;
  48. }
  49. return [r,
  50. g,
  51. b
  52. ];
  53. }
  54. function rgbtoHsv(r, g, b) {
  55. var max = Math.max(r, g, b);
  56. var min = Math.min(r, g, b);
  57. var d = max - min;
  58. var h;
  59. var s = (max === 0 ? 0 : d / max);
  60. var v = max / 255;
  61. switch (max) {
  62. case min:
  63. h = 0;
  64. break;
  65. case r:
  66. h = (g - b) + d * (g < b ? 6 : 0);
  67. h /= 6 * d;
  68. break;
  69. case g:
  70. h = (b - r) + d * 2;
  71. h /= 6 * d;
  72. break;
  73. case b:
  74. h = (r - g) + d * 4;
  75. h /= 6 * d;
  76. break;
  77. default:
  78. break;
  79. }
  80. return [
  81. h,
  82. s,
  83. v,
  84. ];
  85. }
  86. function addSaturationToRgb(color, offset) {
  87. var hsv = rgbtoHsv(color[0] * 255, color[1] * 255, color[2] * 255);
  88. hsv[1] += offset;
  89. if (hsv[1] > 1) {
  90. hsv[1] = 1;
  91. } else if (hsv[1] <= 0) {
  92. hsv[1] = 0;
  93. }
  94. return hsvtoRgb(hsv[0], hsv[1], hsv[2]);
  95. }
  96. function addBrightnessToRgb(color, offset) {
  97. var hsv = rgbtoHsv(color[0] * 255, color[1] * 255, color[2] * 255);
  98. hsv[2] += offset;
  99. if (hsv[2] > 1) {
  100. hsv[2] = 1;
  101. } else if (hsv[2] < 0) {
  102. hsv[2] = 0;
  103. }
  104. return hsvtoRgb(hsv[0], hsv[1], hsv[2]);
  105. }
  106. function addHueToRgb(color, offset) {
  107. var hsv = rgbtoHsv(color[0] * 255, color[1] * 255, color[2] * 255);
  108. hsv[0] += offset / 360;
  109. if (hsv[0] > 1) {
  110. hsv[0] -= 1;
  111. } else if (hsv[0] < 0) {
  112. hsv[0] += 1;
  113. }
  114. return hsvtoRgb(hsv[0], hsv[1], hsv[2]);
  115. }
  116. function rgbToHex() {
  117. var colorMap = [];
  118. var i;
  119. var hex;
  120. for (i = 0; i < 256; i += 1) {
  121. hex = i.toString(16);
  122. colorMap[i] = hex.length === 1 ? '0' + hex : hex;
  123. }
  124. return function(r, g, b) {
  125. if (r < 0) {
  126. r = 0;
  127. }
  128. if (g < 0) {
  129. g = 0;
  130. }
  131. if (b < 0) {
  132. b = 0;
  133. }
  134. return '#' + colorMap[r] + colorMap[g] + colorMap[b];
  135. };
  136. }
  137. function hexToRgb(sColor, str = true) {
  138. let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  139. sColor = sColor.toLowerCase();
  140. if (sColor && reg.test(sColor)) {
  141. if (sColor.length === 4) {
  142. let sColorNew = "#";
  143. for (let i = 1; i < 4; i += 1) {
  144. sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
  145. }
  146. sColor = sColorNew;
  147. }
  148. //处理六位的颜色值
  149. let sColorChange = [];
  150. for (let i = 1; i < 7; i += 2) {
  151. sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
  152. }
  153. if(!str) {
  154. return sColorChange;
  155. } else {
  156. return `rgb(${sColorChange[0]},${sColorChange[1]},${sColorChange[2]})`;
  157. }
  158. } else if (/^(rgb|RGB)/.test(sColor)) {
  159. let arr = sColor.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(",")
  160. return arr.map(val => Number(val));
  161. } else {
  162. return sColor;
  163. }
  164. };
  165. function rgbToHsl(r,g,b){
  166. r=r/255;
  167. g=g/255;
  168. b=b/255;
  169. var min=Math.min(r,g,b);
  170. var max=Math.max(r,g,b);
  171. var l=(min+max)/2;
  172. var difference = max-min;
  173. var h,s,l;
  174. if(max==min){
  175. h=0;
  176. s=0;
  177. }else{
  178. s=l>0.5?difference/(2.0-max-min):difference/(max+min);
  179. switch(max){
  180. case r: h=(g-b)/difference+(g < b ? 6 : 0);break;
  181. case g: h=2.0+(b-r)/difference;break;
  182. case b: h=4.0+(r-g)/difference;break;
  183. }
  184. h=Math.round(h*60);
  185. }
  186. s=Math.round(s*100);//转换成百分比的形式
  187. l=Math.round(l*100);
  188. return [h,s,l];
  189. }
  190. function hslToRgb(h,s,l){
  191. var h=h/360;
  192. var s=s/100;
  193. var l=l/100;
  194. var rgb=[];
  195. if(s==0){
  196. rgb=[Math.round(l*255),Math.round(l*255),Math.round(l*255)];
  197. }else{
  198. var q=l>=0.5?(l+s-l*s):(l*(1+s));
  199. var p=2*l-q;
  200. var tr=rgb[0]=h+1/3;
  201. var tg=rgb[1]=h;
  202. var tb=rgb[2]=h-1/3;
  203. for(var i=0; i<rgb.length;i++){
  204. var tc=rgb[i];
  205. console.log(tc);
  206. if(tc<0){
  207. tc=tc+1;
  208. }else if(tc>1){
  209. tc=tc-1;
  210. }
  211. switch(true){
  212. case (tc<(1/6)):
  213. tc=p+(q-p)*6*tc;
  214. break;
  215. case ((1/6)<=tc && tc<0.5):
  216. tc=q;
  217. break;
  218. case (0.5<=tc && tc<(2/3)):
  219. tc=p+(q-p)*(4-6*tc);
  220. break;
  221. default:
  222. tc=p;
  223. break;
  224. }
  225. rgb[i]=Math.round(tc*255);
  226. }
  227. }
  228. return rgb;
  229. }
  230. /**
  231. * 求两个颜色之间的渐变值
  232. * @param {string} startColor 开始的颜色
  233. * @param {string} endColor 结束的颜色
  234. * @param {number} step 颜色等分的份额
  235. * */
  236. function colorGradient(startColor = 'rgb(0, 0, 0)', endColor = 'rgb(255, 255, 255)', step = 10) {
  237. let startRGB = hexToRgb(startColor, false); //转换为rgb数组模式
  238. let startR = startRGB[0];
  239. let startG = startRGB[1];
  240. let startB = startRGB[2];
  241. let endRGB = hexToRgb(endColor, false);
  242. let endR = endRGB[0];
  243. let endG = endRGB[1];
  244. let endB = endRGB[2];
  245. let sR = (endR - startR) / step; //总差值
  246. let sG = (endG - startG) / step;
  247. let sB = (endB - startB) / step;
  248. let colorArr = [];
  249. for (let i = 0; i < step; i++) {
  250. //计算每一步的hex值
  251. let hex = rgbToHex('rgb(' + Math.round((sR * i + startR)) + ',' + Math.round((sG * i + startG)) + ',' + Math.round((sB *
  252. i + startB)) + ')');
  253. colorArr.push(hex);
  254. }
  255. return colorArr;
  256. }