rgb.js 3.7 KB

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