3 # Calculate compound HSV feColorMatrix for svg filter
4 # http://www.w3.org/TR/SVG11/filters.html#feColorMatrixElement
7 # ./hsv-matrix.pl HUE(degrees) SATURATION VALUE
14 my $hue = $ARGV[0]*3.14159/180.0;
20 my @lum = (0.2126, 0.7152, 0.0722);
21 my @inv = (1.0-$lum[0], 1.0-$lum[1], 1.0-$lum[2]);
24 # Hue transformation matrix
26 [$lum[0]+cos($hue)*$inv[0]-sin($hue)*$lum[0],
27 $lum[1]-cos($hue)*$lum[1]-sin($hue)*$lum[1],
28 $lum[2]-cos($hue)*$lum[2]+sin($hue)*$inv[2]],
30 [$lum[0]-cos($hue)*$lum[0]+sin($hue)*0.143,
31 $lum[1]+cos($hue)*$inv[1]+sin($hue)*0.140,
32 $lum[2]-cos($hue)*$lum[2]-sin($hue)*0.283],
34 [$lum[0]-cos($hue)*$lum[0]-sin($hue)*$inv[0],
35 $lum[1]-cos($hue)*$lum[1]+sin($hue)*$lum[1],
36 $lum[2]+cos($hue)*$inv[2]+sin($hue)*$lum[2]]
40 # Saturation transformation matrix
42 [$lum[0]+$inv[0]*$sat, $lum[1]-$lum[1]*$sat, $lum[2]-$lum[2]*$sat],
43 [$lum[0]-$lum[0]*$sat, $lum[1]+$inv[1]*$sat, $lum[2]-$lum[2]*$sat],
44 [$lum[0]-$lum[0]*$sat, $lum[1]-$lum[1]*$sat, $lum[2]+$inv[2]*$sat]
48 # Print feColorMatrix (HxSxV)
50 printf("$f $f $f 0 0 $f $f $f 0 0 $f $f $f 0 0 0 0 0 1 0\n",
51 ($h[0][0]*$s[0][0]+$h[0][1]*$s[1][0]+$h[0][2]*$s[2][0])*$val,
52 ($h[0][0]*$s[0][1]+$h[0][1]*$s[1][1]+$h[0][2]*$s[2][1])*$val,
53 ($h[0][0]*$s[0][2]+$h[0][1]*$s[1][2]+$h[0][2]*$s[2][2])*$val,
55 ($h[1][0]*$s[0][0]+$h[1][1]*$s[1][0]+$h[1][2]*$s[2][0])*$val,
56 ($h[1][0]*$s[0][1]+$h[1][1]*$s[1][1]+$h[1][2]*$s[2][1])*$val,
57 ($h[1][0]*$s[0][2]+$h[1][1]*$s[1][2]+$h[1][2]*$s[2][2])*$val,
59 ($h[2][0]*$s[0][0]+$h[2][1]*$s[1][0]+$h[2][2]*$s[2][0])*$val,
60 ($h[2][0]*$s[0][1]+$h[2][1]*$s[1][1]+$h[2][2]*$s[2][1])*$val,
61 ($h[2][0]*$s[0][2]+$h[2][1]*$s[1][2]+$h[2][2]*$s[2][2])*$val);