Guest User

Untitled

a guest
Nov 24th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. // Author baixin.zhao@rokid
  2.  
  3. exports.rgb2hsv = function (rgb) {
  4.  
  5. let R, G, B, H, S, V;
  6.  
  7. R = (rgb >> 16) & 0xFF;
  8. G = (rgb >> 8) & 0xFF;
  9. B = rgb & 0xFF;
  10.  
  11. const M = Math.max(R, G, B);
  12. const m = Math.min(R, G, B);
  13.  
  14. V = M / 255; //range to [0, 1]
  15. S = M === 0 ? 0 : (1 - m / M);
  16.  
  17. const num = R - 0.5 * G - 0.5 * B;
  18. const den = Math.sqrt(Math.pow(R, 2) + Math.pow(G, 2) + Math.pow(B, 2) - R * G - R * B - G * B);
  19. let div;
  20. if(den === 0) {
  21. div = 0;
  22. } else {
  23. div = num / den;
  24. }
  25. const count = Math.acos(div) / Math.PI * 180;
  26. if (G >= B) {
  27. H = count;
  28. } else {
  29. H = 360 - count;
  30. }
  31.  
  32. return [H, S, V]
  33. };
  34.  
  35. exports.hsv2rgb = function (hsv) {
  36.  
  37. let R, G, B;
  38.  
  39. let H = hsv[0];
  40. let S = hsv[1];
  41. let V = hsv[2];
  42.  
  43. if (H === 0) {
  44. R = V + 2 * V * S;
  45. G = V - V * S;
  46. B = V - V * S;
  47. } else if (0 < H < 120) {
  48. R = V + V * S * Math.cos(H) / Math.cos(60 - H);
  49. G = V + V * S * (1 - Math.cos(H) / Math.cos(60 - H));
  50. B = V - V * S;
  51. } else if (H === 120) {
  52. R = V - V * S;
  53. G = V + 2 * V * S;
  54. B = V - V * S;
  55. } else if (120 < H < 240) {
  56. R = V - V * S;
  57. G = V + V * S * Math.cos(H - 120) / Math.cos(180 - H);
  58. B = V + V * S * (1 - Math.cos(H - 120) / Math.cos(180 - H));
  59. } else if (H === 240) {
  60. R = V - V * S;
  61. G = V - V * S;
  62. B = V + 2 * V * S;
  63. } else {
  64. R = V + V * S * (1 - Math.cos(H - 240) / Math.cos(300 - H));
  65. G = V - V * S;
  66. B = V + V * S * Math.cos(H - 240) / Math.cos(300 - H);
  67. }
  68.  
  69. return [R, G, B]
  70. };
  71.  
  72. exports.hsv2rgbw = function (hsv) {
  73. let r, g, b, w;
  74. let cos_h, cos_1047_h;
  75.  
  76. let H = hsv[0];
  77. let S = hsv[1];
  78. let V = hsv[2];
  79.  
  80. H = 3.14159 * H / 180; // Convert to radians.
  81. S = S > 0 ? (S < 1 ? S : 1) : 0; // clamp S and I to interval [0,1]
  82. V = V > 0 ? (V < 1 ? V : 1) : 0;
  83.  
  84. if (H < 2.09439) {
  85. cos_h = Math.cos(H);
  86. cos_1047_h = Math.cos(1.047196667 - H);
  87. r = S * 255 * V / 3 * (1 + cos_h / cos_1047_h);
  88. g = S * 255 * V / 3 * (1 + (1 - cos_h / cos_1047_h));
  89. b = 0;
  90. w = 255 * (1 - S) * V;
  91. } else if (H < 4.188787) {
  92. H = H - 2.09439;
  93. cos_h = Math.cos(H);
  94. cos_1047_h = Math.cos(1.047196667 - H);
  95. g = S * 255 * V / 3 * (1 + cos_h / cos_1047_h);
  96. b = S * 255 * V / 3 * (1 + (1 - cos_h / cos_1047_h));
  97. r = 0;
  98. w = 255 * (1 - S) * V;
  99. } else {
  100. H = H - 4.188787;
  101. cos_h = Math.cos(H);
  102. cos_1047_h = Math.cos(1.047196667 - H);
  103. b = S * 255 * V / 3 * (1 + cos_h / cos_1047_h);
  104. r = S * 255 * V / 3 * (1 + (1 - cos_h / cos_1047_h));
  105. g = 0;
  106. w = 255 * (1 - S) * V;
  107. }
  108.  
  109. return [w, r, g, b]
  110. };
  111.  
  112. exports.toNum = function (arr) {
  113. let length = arr.length;
  114. let result = 0;
  115. arr.forEach((item, index) => {
  116. result += Math.round(item) * Math.pow(2, (length - 1 - index) * 8);
  117. });
  118. return result;
  119. };
Add Comment
Please, Sign In to add comment