1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| <img src="image.png">
<script>
/*
https://stackoverflow.com/questions/2348597/why-doesnt-this-javascript-rgb-to-hsl-code-work
Adaptations :
- Remplacement paramètres "r", "g" et "b" par "color".
- Ajout du cas où r=g=b.
- Suppression des "Math.floor".
- Retour d'un Object à la place du Array.
*/
function rgbToHsv(color)
{
var
r = color.r,
g = color.g,
b = color.b;
if(r===g && g===b)
{
return {h : 0,s : 0,v : 100 * r / 255};
}
var
min = Math.min(r, g, b),
max = Math.max(r, g, b),
delta = max - min,
h, s, v = max;
v = max / 255 * 100;
s = delta / max * 100;
if( r == max )
{
h = ( g - b ) / delta;
}
else if( g == max )
{
h = 2 + ( b - r ) / delta;
}
else
{
h = 4 + ( r - g ) / delta;
}
h = h * 60;
if( h < 0 )
{
h += 360;
}
return {h : h,s : s,v : v};
}
/*
https://github.com/WebKit/webkit/blob/fb3c15607f80e19d7d3773e0cbcabe5bb2d6125e/Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp
Adaptations :
- Réécriture en js.
- Suppression alpha.
*/
function applyHueRotateFilter(color, u_filterAmount)
{
var c = Math.cos(u_filterAmount * Math.PI / 180.0);
var s = Math.sin(u_filterAmount * Math.PI / 180.0);
return {
r : color.r * (0.213 + c * 0.787 - s * 0.213) + color.g * (0.715 - c * 0.715 - s * 0.715) + color.b * (0.072 - c * 0.072 + s * 0.928),
g : color.r * (0.213 - c * 0.213 + s * 0.143) + color.g * (0.715 + c * 0.285 + s * 0.140) + color.b * (0.072 - c * 0.072 - s * 0.283),
b : color.r * (0.213 - c * 0.213 - s * 0.787) + color.g * (0.715 - c * 0.715 + s * 0.715) + color.b * (0.072 + c * 0.928 + s * 0.072)
};
}
function applySaturateFilter(color, u_filterAmount)
{
return {
r : (0.213 + 0.787 * u_filterAmount) * color.r + (0.715 - 0.715 * u_filterAmount) * color.g + (0.072 - 0.072 * u_filterAmount) * color.b,
g : (0.213 - 0.213 * u_filterAmount) * color.r + (0.715 + 0.285 * u_filterAmount) * color.g + (0.072 - 0.072 * u_filterAmount) * color.b,
b : (0.213 - 0.213 * u_filterAmount) * color.r + (0.715 - 0.715 * u_filterAmount) * color.g + (0.072 + 0.928 * u_filterAmount) * color.b
};
}
function applyBrightnessFilter(color, u_filterAmount)
{
return {
r : color.r * u_filterAmount,
g : color.g * u_filterAmount,
b : color.b * u_filterAmount
};
}
/*
Fonction que j'avais récupérée (mais où ?) et adaptée.
*/
function RGB(color)
{
return {
r:(color>>16)&0xFF,
g:(color>>8)&0xFF,
b:color&0xFF
};
}
/*
Ma solution par rapprochement.
*/
function hsvFilter(color)
{
var color_rgb;
var color_hsl;
var colorInit_rgb=RGB(0xFF0000);
var colorInit_hsl=rgbToHsv(colorInit_rgb);
var colorFina_rgb=RGB(color);
var colorFina_hsl=rgbToHsv(colorFina_rgb);
var h=colorFina_hsl.h-colorInit_hsl.h;
var s=colorFina_hsl.s/colorInit_hsl.s;
var v=colorFina_hsl.v/colorInit_hsl.v;
if(colorFina_rgb.r===colorFina_rgb.g && colorFina_rgb.g===colorFina_rgb.b)
{
s=0;
if(colorFina_rgb.r===0)
{
return {h:0,s:0,v:0};
}
}
var i=-1; //utilisation d'un compteur par sécurité (à priori inutile)
do
{
color_rgb=applyHueRotateFilter(colorInit_rgb,h);
color_rgb=applySaturateFilter(color_rgb,s);
color_rgb=applyBrightnessFilter(color_rgb,v);
color_hsl=rgbToHsv(color_rgb);
h+=colorFina_hsl.h-color_hsl.h;
if(s!==0)
{
s*=colorFina_hsl.s/color_hsl.s;
}
v*=colorFina_hsl.v/color_hsl.v;
}
while(++i<1000 && (Math.abs(colorFina_hsl.h-color_hsl.h)>0.1 || Math.abs(colorFina_hsl.s-color_hsl.s)>0.1 || Math.abs(colorFina_hsl.v-color_hsl.v)>0.1));
console.log(i,Math.abs(colorFina_hsl.h-color_hsl.h),Math.abs(colorFina_hsl.s-color_hsl.s),Math.abs(colorFina_hsl.v-color_hsl.v)); //i maximal observé : 54
console.log("colorFina_rgb",colorFina_rgb);
console.log("colorFina_hsl",colorFina_hsl);
console.log("color_rgb",color_rgb);
console.log("color_hsl",color_hsl);
return {h:h,s:s,v:v};
}
var ob_=hsvFilter(0x6581A8);
document.getElementsByTagName("img")[0].style.filter="hue-rotate("+ob_.h+"deg) saturate("+ob_.s+") brightness("+ob_.v+")";
</script> |
Partager