A new version with a precise method
function rgbToHsv(rgb) { let r = rgb[0] / 255, g = rgb[1] / 255, b = rgb[2] / 255; const v = Math.max(r, g, b); const min = Math.min(r, g, b); const s = v === 0 ? 0 : (v - min) / v; let h = 0; if (s !== 0) { if (v === r) { h = (g - b) / (v - min); } else if (v === g) { h = 2 + (b - r) / (v - min); } else { h = 4 + (r - g) / (v - min); } h *= 60; if (h < 0) h += 360; } return Array.of(Math.round(h), Math.round(s * 100), Math.round(v * 100)); }
- function rgbToHsv(rgb) {
let r = rgb[0], g = rgb[1], b = rgb[2];const v = Math.max(r, g, b) / 255;if (v == 0) return Array.of(0, 0, 0);r /= v;g /= v;b /= v;const s = 1 - Math.min(r, g, b) / 255;if (s == 0) return Array.of(0, 0, v * 100);r = 255 - (255 - r) / s;g = 255 - (255 - g) / s;b = 255 - (255 - b) / s;const peak = Math.max(r, g, b);- let r = rgb[0] / 255, g = rgb[1] / 255, b = rgb[2] / 255;
- const v = Math.max(r, g, b);
- const min = Math.min(r, g, b);
- const s = v === 0 ? 0 : (v - min) / v;
- let h = 0;
if (r == peak) h = g < b ? 360 - b * 60 / 255 : g * 60 / 255;else if (g == peak) h = r > b ? 120 - r * 60 / 255 : 120 + b * 60 / 255;else h = r > g ? 240 + r * 60 / 255 : 240 - g * 60 / 255;- if (s !== 0) {
- if (v === r) {
- h = (g - b) / (v - min);
- } else if (v === g) {
- h = 2 + (b - r) / (v - min);
- } else {
- h = 4 + (r - g) / (v - min);
- }
- h *= 60;
- if (h < 0) h += 360;
- }
- return Array.of(Math.round(h), Math.round(s * 100), Math.round(v * 100));