rgb.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.fromCIELab = exports.fromYCbCr = exports.fromCMYK = exports.fromPalette = exports.fromBlackIsZero = exports.fromWhiteIsZero = void 0;
  4. function fromWhiteIsZero(raster, max) {
  5. const { width, height } = raster;
  6. const rgbRaster = new Uint8Array(width * height * 3);
  7. let value;
  8. for (let i = 0, j = 0; i < raster.length; ++i, j += 3) {
  9. value = 256 - (raster[i] / max * 256);
  10. rgbRaster[j] = value;
  11. rgbRaster[j + 1] = value;
  12. rgbRaster[j + 2] = value;
  13. }
  14. return rgbRaster;
  15. }
  16. exports.fromWhiteIsZero = fromWhiteIsZero;
  17. function fromBlackIsZero(raster, max) {
  18. const { width, height } = raster;
  19. const rgbRaster = new Uint8Array(width * height * 3);
  20. let value;
  21. for (let i = 0, j = 0; i < raster.length; ++i, j += 3) {
  22. value = raster[i] / max * 256;
  23. rgbRaster[j] = value;
  24. rgbRaster[j + 1] = value;
  25. rgbRaster[j + 2] = value;
  26. }
  27. return rgbRaster;
  28. }
  29. exports.fromBlackIsZero = fromBlackIsZero;
  30. function fromPalette(raster, colorMap) {
  31. const { width, height } = raster;
  32. const rgbRaster = new Uint8Array(width * height * 3);
  33. const greenOffset = colorMap.length / 3;
  34. const blueOffset = colorMap.length / 3 * 2;
  35. for (let i = 0, j = 0; i < raster.length; ++i, j += 3) {
  36. const mapIndex = raster[i];
  37. rgbRaster[j] = colorMap[mapIndex] / 65536 * 256;
  38. rgbRaster[j + 1] = colorMap[mapIndex + greenOffset] / 65536 * 256;
  39. rgbRaster[j + 2] = colorMap[mapIndex + blueOffset] / 65536 * 256;
  40. }
  41. return rgbRaster;
  42. }
  43. exports.fromPalette = fromPalette;
  44. function fromCMYK(cmykRaster) {
  45. const { width, height } = cmykRaster;
  46. const rgbRaster = new Uint8Array(width * height * 3);
  47. for (let i = 0, j = 0; i < cmykRaster.length; i += 4, j += 3) {
  48. const c = cmykRaster[i];
  49. const m = cmykRaster[i + 1];
  50. const y = cmykRaster[i + 2];
  51. const k = cmykRaster[i + 3];
  52. rgbRaster[j] = 255 * ((255 - c) / 256) * ((255 - k) / 256);
  53. rgbRaster[j + 1] = 255 * ((255 - m) / 256) * ((255 - k) / 256);
  54. rgbRaster[j + 2] = 255 * ((255 - y) / 256) * ((255 - k) / 256);
  55. }
  56. return rgbRaster;
  57. }
  58. exports.fromCMYK = fromCMYK;
  59. function fromYCbCr(yCbCrRaster) {
  60. const { width, height } = yCbCrRaster;
  61. const rgbRaster = new Uint8ClampedArray(width * height * 3);
  62. for (let i = 0, j = 0; i < yCbCrRaster.length; i += 3, j += 3) {
  63. const y = yCbCrRaster[i];
  64. const cb = yCbCrRaster[i + 1];
  65. const cr = yCbCrRaster[i + 2];
  66. rgbRaster[j] = (y + (1.40200 * (cr - 0x80)));
  67. rgbRaster[j + 1] = (y - (0.34414 * (cb - 0x80)) - (0.71414 * (cr - 0x80)));
  68. rgbRaster[j + 2] = (y + (1.77200 * (cb - 0x80)));
  69. }
  70. return rgbRaster;
  71. }
  72. exports.fromYCbCr = fromYCbCr;
  73. const Xn = 0.95047;
  74. const Yn = 1.00000;
  75. const Zn = 1.08883;
  76. // from https://github.com/antimatter15/rgb-lab/blob/master/color.js
  77. function fromCIELab(cieLabRaster) {
  78. const { width, height } = cieLabRaster;
  79. const rgbRaster = new Uint8Array(width * height * 3);
  80. for (let i = 0, j = 0; i < cieLabRaster.length; i += 3, j += 3) {
  81. const L = cieLabRaster[i + 0];
  82. const a_ = cieLabRaster[i + 1] << 24 >> 24; // conversion from uint8 to int8
  83. const b_ = cieLabRaster[i + 2] << 24 >> 24; // same
  84. let y = (L + 16) / 116;
  85. let x = (a_ / 500) + y;
  86. let z = y - (b_ / 200);
  87. let r;
  88. let g;
  89. let b;
  90. x = Xn * ((x * x * x > 0.008856) ? x * x * x : (x - (16 / 116)) / 7.787);
  91. y = Yn * ((y * y * y > 0.008856) ? y * y * y : (y - (16 / 116)) / 7.787);
  92. z = Zn * ((z * z * z > 0.008856) ? z * z * z : (z - (16 / 116)) / 7.787);
  93. r = (x * 3.2406) + (y * -1.5372) + (z * -0.4986);
  94. g = (x * -0.9689) + (y * 1.8758) + (z * 0.0415);
  95. b = (x * 0.0557) + (y * -0.2040) + (z * 1.0570);
  96. r = (r > 0.0031308) ? ((1.055 * (r ** (1 / 2.4))) - 0.055) : 12.92 * r;
  97. g = (g > 0.0031308) ? ((1.055 * (g ** (1 / 2.4))) - 0.055) : 12.92 * g;
  98. b = (b > 0.0031308) ? ((1.055 * (b ** (1 / 2.4))) - 0.055) : 12.92 * b;
  99. rgbRaster[j] = Math.max(0, Math.min(1, r)) * 255;
  100. rgbRaster[j + 1] = Math.max(0, Math.min(1, g)) * 255;
  101. rgbRaster[j + 2] = Math.max(0, Math.min(1, b)) * 255;
  102. }
  103. return rgbRaster;
  104. }
  105. exports.fromCIELab = fromCIELab;
  106. //# sourceMappingURL=rgb.js.map