index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /**
  2. * FeHelper 颜色转换工具
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. fromHEX: '#43ad7f7f',
  8. fromRGB: '', // Will be calculated
  9. fromHSL: '', // Will be calculated
  10. fromHSV: '', // Will be calculated
  11. toHEX: '',
  12. toRGB: '',
  13. toHSL: '',
  14. toHSV: '',
  15. // Outputs specifically for HSL/HSV rows to avoid confusion
  16. // These are no longer needed with the module layout
  17. /*
  18. toRGB_fromHSL: '',
  19. toHEX_fromHSL: '',
  20. toRGB_fromHSV: '',
  21. toHEX_fromHSV: '',
  22. */
  23. // Internal representation (RGBA object)
  24. color: { r: 0, g: 0, b: 0, a: 1 },
  25. alphaPercent: 100 // For slider binding (0-100)
  26. },
  27. mounted: function () {
  28. this.updateFromHEX(); // Initial calculation
  29. },
  30. beforeDestroy: function() {
  31. // Remove picker cleanup logic
  32. },
  33. methods: {
  34. // Remove initColorPicker method
  35. /*
  36. initColorPicker: function() { ... },
  37. */
  38. // --- Input Update Handlers ---
  39. updateFromHEX: function() {
  40. // Remove picker update logic
  41. /*
  42. if (!this.isUpdatingFromPicker && this.pickerInstance) {
  43. this.pickerInstance.setColor(this.fromHEX, true);
  44. }
  45. */
  46. const result = this.parseHEX(this.fromHEX);
  47. if (result) {
  48. this.color = result;
  49. this.updateAllOutputs(); // Removed parameter
  50. } else {
  51. this.clearOutputs();
  52. }
  53. },
  54. updateFromRGB: function() {
  55. // Remove picker update logic
  56. /*
  57. if (this.pickerInstance) {
  58. const parsed = this.parseRGB(this.fromRGB);
  59. if(parsed) {
  60. const hexObj = this.rgbToHex(parsed.r, parsed.g, parsed.b, parsed.a);
  61. this.pickerInstance.setColor(this.hexToString(hexObj), true);
  62. }
  63. }
  64. */
  65. const result = this.parseRGB(this.fromRGB);
  66. if (result) {
  67. this.color = result;
  68. this.updateAllOutputs();
  69. } else {
  70. this.clearOutputs();
  71. }
  72. },
  73. updateFromHSL: function() {
  74. // Remove picker update logic
  75. /*
  76. if (this.pickerInstance) {
  77. const parsed = this.parseHSL(this.fromHSL);
  78. if(parsed) {
  79. const rgb = this.hslToRgb(parsed.h, parsed.s, parsed.l, parsed.a);
  80. const hexObj = this.rgbToHex(rgb.r, rgb.g, rgb.b, rgb.a);
  81. this.pickerInstance.setColor(this.hexToString(hexObj), true);
  82. }
  83. }
  84. */
  85. const result = this.parseHSL(this.fromHSL);
  86. if (result) {
  87. this.color = this.hslToRgb(result.h, result.s, result.l, result.a);
  88. this.updateAllOutputs();
  89. } else {
  90. this.clearOutputs();
  91. }
  92. },
  93. updateFromHSV: function() {
  94. // Remove picker update logic
  95. /*
  96. if (this.pickerInstance) {
  97. const parsed = this.parseHSV(this.fromHSV);
  98. if(parsed) {
  99. const rgb = this.hsvToRgb(parsed.h, parsed.s, parsed.v, parsed.a);
  100. const hexObj = this.rgbToHex(rgb.r, rgb.g, rgb.b, rgb.a);
  101. this.pickerInstance.setColor(this.hexToString(hexObj), true);
  102. }
  103. }
  104. */
  105. const result = this.parseHSV(this.fromHSV);
  106. if (result) {
  107. this.color = this.hsvToRgb(result.h, result.s, result.v, result.a);
  108. this.updateAllOutputs();
  109. } else {
  110. this.clearOutputs();
  111. }
  112. },
  113. updateAlphaFromSlider: function() {
  114. if (this.color) {
  115. this.color.a = this.alphaPercent / 100;
  116. // Remove picker update logic
  117. /*
  118. if (this.pickerInstance) {
  119. const hexObj = this.rgbToHex(this.color.r, this.color.g, this.color.b, this.color.a);
  120. this.pickerInstance.setColor(this.hexToString(hexObj), true);
  121. }
  122. */
  123. this.updateAllOutputs();
  124. }
  125. },
  126. // --- Update All Outputs ---
  127. updateAllOutputs: function() {
  128. if (!this.color) return;
  129. const { r, g, b, a } = this.color;
  130. this.alphaPercent = Math.round(a * 100);
  131. const hexObj = this.rgbToHex(r, g, b, a);
  132. const rgbString = this.rgbaToString(r, g, b, a);
  133. const hslObj = this.rgbToHsl(r, g, b, a);
  134. const hsvObj = this.rgbToHsv(r, g, b, a);
  135. const newHEX = this.hexToString(hexObj);
  136. const newRGB = rgbString;
  137. const newHSL = this.hslToString(hslObj);
  138. const newHSV = this.hsvToString(hsvObj);
  139. // Update output models (directly use these in template)
  140. this.toHEX = newHEX;
  141. this.toRGB = newRGB;
  142. this.toHSL = newHSL;
  143. this.toHSV = newHSV;
  144. // Update inputs (only if they differ to avoid cursor jumps)
  145. if (this.fromHEX !== newHEX) this.fromHEX = newHEX;
  146. if (this.fromRGB !== newRGB) this.fromRGB = newRGB;
  147. if (this.fromHSL !== newHSL) this.fromHSL = newHSL;
  148. if (this.fromHSV !== newHSV) this.fromHSV = newHSV;
  149. // Update the color picker IF the change didn't originate from it
  150. // Remove picker update logic
  151. /*
  152. if (!this.isUpdatingFromPicker && this.pickerInstance) {
  153. // Use HEX8 format which a-color-picker likely prefers
  154. this.pickerInstance.setColor(newHEX, true);
  155. }
  156. */
  157. },
  158. clearOutputs: function() {
  159. this.toHEX = '';
  160. this.toRGB = '';
  161. this.toHSL = '';
  162. this.toHSV = '';
  163. // Remove clears for specific row outputs
  164. /*
  165. this.toRGB_fromHSL = '';
  166. this.toHEX_fromHSL = '';
  167. this.toRGB_fromHSV = '';
  168. this.toHEX_fromHSV = '';
  169. */
  170. this.alphaPercent = 100;
  171. },
  172. // --- Parsers ---
  173. parseHEX: function(hex) {
  174. hex = hex.trim().replace(/^#/, '');
  175. if (!/^[0-9a-fA-F]+$/.test(hex) || ![3, 4, 6, 8].includes(hex.length)) {
  176. return null;
  177. }
  178. let r, g, b, a = 1;
  179. if (hex.length === 3 || hex.length === 4) {
  180. r = parseInt(hex[0] + hex[0], 16);
  181. g = parseInt(hex[1] + hex[1], 16);
  182. b = parseInt(hex[2] + hex[2], 16);
  183. if (hex.length === 4) a = parseInt(hex[3] + hex[3], 16) / 255;
  184. } else { // 6 or 8
  185. r = parseInt(hex.substring(0, 2), 16);
  186. g = parseInt(hex.substring(2, 4), 16);
  187. b = parseInt(hex.substring(4, 6), 16);
  188. if (hex.length === 8) a = parseInt(hex.substring(6, 8), 16) / 255;
  189. }
  190. if (isNaN(r) || isNaN(g) || isNaN(b) || isNaN(a)) return null;
  191. return { r, g, b, a };
  192. },
  193. parseRGB: function(rgbStr) {
  194. rgbStr = rgbStr.trim().toLowerCase();
  195. const match = rgbStr.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([0-9\.]+))?\s*\)$/);
  196. if (!match) return null;
  197. const r = parseInt(match[1], 10);
  198. const g = parseInt(match[2], 10);
  199. const b = parseInt(match[3], 10);
  200. let a = 1;
  201. if (match[4] !== undefined) {
  202. a = parseFloat(match[4]);
  203. }
  204. if (r > 255 || g > 255 || b > 255 || a < 0 || a > 1 || isNaN(r) || isNaN(g) || isNaN(b) || isNaN(a)) {
  205. return null;
  206. }
  207. return { r, g, b, a };
  208. },
  209. parseHSL: function(hslStr) {
  210. hslStr = hslStr.trim().toLowerCase();
  211. const match = hslStr.match(/^hsla?\(\s*([0-9\.]+)\s*,\s*([0-9\.]+)%?\s*,\s*([0-9\.]+)%?\s*(?:,\s*([0-9\.]+))?\s*\)$/);
  212. if (!match) return null;
  213. const h = parseFloat(match[1]);
  214. const s = parseFloat(match[2]);
  215. const l = parseFloat(match[3]);
  216. let a = 1;
  217. if (match[4] !== undefined) {
  218. a = parseFloat(match[4]);
  219. }
  220. if (h < 0 || h > 360 || s < 0 || s > 100 || l < 0 || l > 100 || a < 0 || a > 1 || isNaN(h) || isNaN(s) || isNaN(l) || isNaN(a)) {
  221. return null;
  222. }
  223. return { h, s, l, a };
  224. },
  225. parseHSV: function(hsvStr) {
  226. hsvStr = hsvStr.trim().toLowerCase();
  227. // Assuming HSV format like hsv(H, S%, V%) or hsva(H, S%, V%, A)
  228. const match = hsvStr.match(/^hsva?\(\s*([0-9\.]+)\s*,\s*([0-9\.]+)%?\s*,\s*([0-9\.]+)%?\s*(?:,\s*([0-9\.]+))?\s*\)$/);
  229. if (!match) return null;
  230. const h = parseFloat(match[1]);
  231. const s = parseFloat(match[2]);
  232. const v = parseFloat(match[3]);
  233. let a = 1;
  234. if (match[4] !== undefined) {
  235. a = parseFloat(match[4]);
  236. }
  237. if (h < 0 || h > 360 || s < 0 || s > 100 || v < 0 || v > 100 || a < 0 || a > 1 || isNaN(h) || isNaN(s) || isNaN(v) || isNaN(a)) {
  238. return null;
  239. }
  240. return { h, s, v, a };
  241. },
  242. // --- Conversion Functions (RGB as Base) ---
  243. // RGB -> HEX
  244. rgbToHex: function(r, g, b, a) {
  245. const toHex = (c) => parseInt(c, 10).toString(16).padStart(2, '0');
  246. const hex = `#${toHex(r)}${toHex(g)}${toHex(b)}`;
  247. if (a < 1) {
  248. const alphaHex = Math.round(a * 255).toString(16).padStart(2, '0');
  249. return { hex: hex + alphaHex, hasAlpha: true };
  250. }
  251. return { hex: hex, hasAlpha: false };
  252. },
  253. // RGB -> HSL
  254. rgbToHsl: function(r, g, b, a) {
  255. r /= 255; g /= 255; b /= 255;
  256. const max = Math.max(r, g, b), min = Math.min(r, g, b);
  257. let h, s, l = (max + min) / 2;
  258. if (max === min) {
  259. h = s = 0; // achromatic
  260. } else {
  261. const d = max - min;
  262. s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  263. switch (max) {
  264. case r: h = (g - b) / d + (g < b ? 6 : 0); break;
  265. case g: h = (b - r) / d + 2; break;
  266. case b: h = (r - g) / d + 4; break;
  267. }
  268. h /= 6;
  269. }
  270. return { h: h * 360, s: s * 100, l: l * 100, a: a };
  271. },
  272. // RGB -> HSV
  273. rgbToHsv: function(r, g, b, a) {
  274. r /= 255; g /= 255; b /= 255;
  275. const max = Math.max(r, g, b), min = Math.min(r, g, b);
  276. let h, s, v = max;
  277. const d = max - min;
  278. s = max === 0 ? 0 : d / max;
  279. if (max === min) {
  280. h = 0; // achromatic
  281. } else {
  282. switch (max) {
  283. case r: h = (g - b) / d + (g < b ? 6 : 0); break;
  284. case g: h = (b - r) / d + 2; break;
  285. case b: h = (r - g) / d + 4; break;
  286. }
  287. h /= 6;
  288. }
  289. return { h: h * 360, s: s * 100, v: v * 100, a: a };
  290. },
  291. // HSL -> RGB
  292. hslToRgb: function(h, s, l, a) {
  293. h /= 360; s /= 100; l /= 100;
  294. let r, g, b;
  295. if (s === 0) {
  296. r = g = b = l; // achromatic
  297. } else {
  298. const hue2rgb = (p, q, t) => {
  299. if (t < 0) t += 1;
  300. if (t > 1) t -= 1;
  301. if (t < 1/6) return p + (q - p) * 6 * t;
  302. if (t < 1/2) return q;
  303. if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
  304. return p;
  305. };
  306. const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
  307. const p = 2 * l - q;
  308. r = hue2rgb(p, q, h + 1/3);
  309. g = hue2rgb(p, q, h);
  310. b = hue2rgb(p, q, h - 1/3);
  311. }
  312. return { r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255), a: a };
  313. },
  314. // HSV -> RGB
  315. hsvToRgb: function(h, s, v, a) {
  316. h /= 360; s /= 100; v /= 100;
  317. let r, g, b;
  318. const i = Math.floor(h * 6);
  319. const f = h * 6 - i;
  320. const p = v * (1 - s);
  321. const q = v * (1 - f * s);
  322. const t = v * (1 - (1 - f) * s);
  323. switch (i % 6) {
  324. case 0: r = v; g = t; b = p; break;
  325. case 1: r = q; g = v; b = p; break;
  326. case 2: r = p; g = v; b = t; break;
  327. case 3: r = p; g = q; b = v; break;
  328. case 4: r = t; g = p; b = v; break;
  329. case 5: r = v; g = p; b = q; break;
  330. }
  331. return { r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255), a: a };
  332. },
  333. // --- Formatters ---
  334. rgbaToString: function(r, g, b, a) {
  335. if (a < 1) {
  336. return `rgba(${r}, ${g}, ${b}, ${a.toFixed(2).replace(/\.0+$/,'')})`;
  337. } else {
  338. return `rgb(${r}, ${g}, ${b})`;
  339. }
  340. },
  341. hexToString: function(hexObj) {
  342. return hexObj.hex;
  343. },
  344. hslToString: function(hslObj) {
  345. const h = hslObj.h.toFixed(0);
  346. const s = hslObj.s.toFixed(1).replace(/\.0$/,'');
  347. const l = hslObj.l.toFixed(1).replace(/\.0$/,'');
  348. if (hslObj.a < 1) {
  349. return `hsla(${h}, ${s}%, ${l}%, ${hslObj.a.toFixed(2).replace(/\.0+$/,'')})`;
  350. } else {
  351. return `hsl(${h}, ${s}%, ${l}%)`;
  352. }
  353. },
  354. hsvToString: function(hsvObj) {
  355. const h = hsvObj.h.toFixed(0);
  356. const s = hsvObj.s.toFixed(1).replace(/\.0$/,'');
  357. const v = hsvObj.v.toFixed(1).replace(/\.0$/,'');
  358. if (hsvObj.a < 1) {
  359. return `hsva(${h}, ${s}%, ${v}%, ${hsvObj.a.toFixed(2).replace(/\.0+$/,'')})`;
  360. } else {
  361. return `hsv(${h}, ${s}%, ${v}%)`;
  362. }
  363. },
  364. // --- Utilities ---
  365. copyToClipboard: function(text) {
  366. if (text && navigator.clipboard) {
  367. navigator.clipboard.writeText(text).then(() => {
  368. // Optional: Show feedback to user
  369. // console.log('Copied:', text);
  370. }).catch(err => {
  371. console.error('Failed to copy text: ', err);
  372. });
  373. }
  374. },
  375. safeBgColor: function() {
  376. // Ignore the input colorStr, always use the internal RGBA color object
  377. if (this.color && typeof this.color.r === 'number') {
  378. // Generate an RGBA string which is universally supported by browsers
  379. const { r, g, b, a } = this.color;
  380. return `rgba(${r}, ${g}, ${b}, ${a})`;
  381. } else {
  382. // Return transparent or a default if the internal color is invalid
  383. return 'transparent';
  384. }
  385. },
  386. openDonateModal: function(event) {
  387. event.preventDefault();
  388. event.stopPropagation();
  389. chrome.runtime.sendMessage({
  390. type: 'fh-dynamic-any-thing',
  391. thing: 'open-donate-modal',
  392. params: { toolName: 'trans-color' }
  393. });
  394. },
  395. openOptionsPage: function(event) {
  396. event.preventDefault();
  397. event.stopPropagation();
  398. chrome.runtime.openOptionsPage();
  399. }
  400. }
  401. });