## yiq.pkg
#
# YIQ NTSC colorspace -- see http://en.wikipedia.org/wiki/YIQ
# Compiled by:
#
src/lib/x-kit/xclient/xclient-internals.sublibstipulate
include package rw_float_vector; # Enable vec[i] and vec[i] := f notations.
#
package fv = rw_float_vector; # rw_float_vector is from
src/lib/std/rw-float-vector.pkgherein
package yiq
: Yiq # Yiq is from
src/lib/x-kit/xclient/src/color/yiq.api {
# We represent a YIQ color value by a
# packed 3-vector of 64-bit floats holding
# red, green, blue in that order:
#
Yiq = fv::Rw_Vector; # This should really be a read-only floatvector, but currently they are not actually a packed type -- See
src/lib/std/src/vector-of-eight-byte-floats.pkg XXX BUGGO FIXME.
# # Since we export Rgb as an opaque type, the difference is not critical.
fun from_floats { y, i, q } # Should do some sort of validation (restriction to [0,1) interval). What exception should we throw? Or should we silently truncate? XXX BUGGO FIXME.
=
{ yiq = fv::make_rw_vector (3, 0.0);
yiq[0] := y; # Eventually we'll probably want to suppress index checking here for speed, using unsafe:: operations or whatever. XXX BUGGO FIXME.
yiq[1] := i;
yiq[2] := q;
yiq;
};
fun to_floats yiq
=
{ y = yiq[0]; # Eventually we'll probably want to suppress index checking here for speed, using unsafe:: operations or whatever. XXX BUGGO FIXME.
i = yiq[1];
q = yiq[2];
{ y, i, q };
};
fun from_rgb rgb
=
{ (rgb::rgb_to_floats rgb) -> (red, green, blue);
y = 0.30*red + 0.59*green + 0.11*blue;
i = 0.60*red - 0.28*green - 0.32*blue;
q = 0.21*red - 0.52*green + 0.31*blue;
from_floats { y, i, q };
};
fun from_name colorname
=
from_rgb (rgb::rgb_from_floats (x11_color_name::to_floats colorname));
};
end;