scripting_utils.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.ColorConverters = void 0;
  27. function makeColorComp(n) {
  28. return Math.floor(Math.max(0, Math.min(1, n)) * 255).toString(16).padStart(2, "0");
  29. }
  30. class ColorConverters {
  31. static CMYK_G([c, y, m, k]) {
  32. return ["G", 1 - Math.min(1, 0.3 * c + 0.59 * m + 0.11 * y + k)];
  33. }
  34. static G_CMYK([g]) {
  35. return ["CMYK", 0, 0, 0, 1 - g];
  36. }
  37. static G_RGB([g]) {
  38. return ["RGB", g, g, g];
  39. }
  40. static G_HTML([g]) {
  41. const G = makeColorComp(g);
  42. return `#${G}${G}${G}`;
  43. }
  44. static RGB_G([r, g, b]) {
  45. return ["G", 0.3 * r + 0.59 * g + 0.11 * b];
  46. }
  47. static RGB_HTML([r, g, b]) {
  48. const R = makeColorComp(r);
  49. const G = makeColorComp(g);
  50. const B = makeColorComp(b);
  51. return `#${R}${G}${B}`;
  52. }
  53. static T_HTML() {
  54. return "#00000000";
  55. }
  56. static CMYK_RGB([c, y, m, k]) {
  57. return ["RGB", 1 - Math.min(1, c + k), 1 - Math.min(1, m + k), 1 - Math.min(1, y + k)];
  58. }
  59. static CMYK_HTML(components) {
  60. const rgb = this.CMYK_RGB(components).slice(1);
  61. return this.RGB_HTML(rgb);
  62. }
  63. static RGB_CMYK([r, g, b]) {
  64. const c = 1 - r;
  65. const m = 1 - g;
  66. const y = 1 - b;
  67. const k = Math.min(c, m, y);
  68. return ["CMYK", c, m, y, k];
  69. }
  70. }
  71. exports.ColorConverters = ColorConverters;