index.js 17 KB

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