PreviousUpNext

15.4.1583  src/lib/x-kit/xclient/src/color/rgb8.pkg

## rgb8.pkg
#
# RGB color representations at 8 bits per color component,
# 24 bits total.
#
# I'm currently hardwiring this format as
#
#     red   = 0xFF0000
#     green = 0x00FF00
#     blue  = 0x0000FF
#
# Technically, X allows any number of bits per
# color value, different numbers of bits per color
# value, arbitrary ordering of color fields etc,
# but my impression is that in practice everyone
# is using the above format these days.
#
# See also:
#     src/lib/x-kit/xclient/src/color/hue-saturation-value.pkg
#     src/lib/x-kit/xclient/src/color/rgb.pkg

# Compiled by:
#     src/lib/x-kit/xclient/xclient-internals.sublib




stipulate
    package f8b =  eight_byte_float;                                    # eight_byte_float      is from   src/lib/std/eight-byte-float.pkg
herein


    package rgb8
    :       Rgb8                                                        # Rgb8                  is from   src/lib/x-kit/xclient/src/color/rgb8.api
    {
        #
        Rgb8 = Int;


        fun rgb8_from_int i
            =
            i;


        fun rgb8_to_int i
            =
            i;


        fun rgb8_from_ints (r, g, b)
            =
            {   r = r & 0xFF;
                g = g & 0xFF;
                b = b & 0xFF;

                i = (r << 16)
                  | (g <<  8)
                  | (b      )
                  ;

                i;
            };

        fun rgb8_to_ints i
            =
            {   r = (i >> 16); 
                g = (i >>  8) & 0xFF; 
                b = (i      ) & 0xFF; 

                (r, g, b);
            };


        fun rgb8_from_floats (red, green, blue)
            =
            rgb8_from_ints
              ( float_to_int  red,
                float_to_int  green,
                float_to_int  blue
              )
            where
                fun float_to_int f
                    =
                    {   f =  (f < 0.0) ?? 0.0 :: f;
                        f =  (f > 1.0) ?? 1.0 :: f;

                        f =   f * 255.99;               # Our ints run 0 -> 255.

                        f8b::truncate  f;
                    };
            end;


        fun rgb8_to_floats  rgb8
            =
            {   (rgb8_to_ints  rgb8)
                    ->
                    (red, green, blue);

                ( int_to_float  red,
                  int_to_float  green,
                  int_to_float  blue
                );
            }
            where
                fun int_to_float i
                    =
                    {   f  = f8b::from_int  i;
                        #
                        f  = f / 255.0;                 # Our ints run 0 -> 255.

                        f;
                    };
            end;


        fun rgb8_from_rgb  rgb
            =
            rgb8_from_floats (rgb::rgb_to_floats rgb);


        fun rgb8_to_rgb  rgb8
            =
            rgb::rgb_from_floats (rgb8_to_floats  rgb8);


        fun same_rgb8 (i, j)
            =
            i == j;


        fun rgb8_from_name  colorname
            =
            rgb8_from_ints  (x11_color_name::to_ints  colorname);


        rgb8_color0  = rgb8_from_int 0;                         # At present we need these irritating rgb8_* prefixes because
        rgb8_color1  = rgb8_from_int 1;                         # 
        #                                                       #      src/lib/x-kit/xclient/xclient.pkg
        rgb8_white   = rgb8_from_int 0xFFFFFF;                  #
        rgb8_black   = rgb8_from_int 0x000000;                  # dumps rgb.pkg and reg8.pkg into the same namespace. :-(   XXX SUCKO FIXME.
        #
        rgb8_red     = rgb8_from_int 0xFF0000;
        rgb8_green   = rgb8_from_int 0x00FF00;
        rgb8_blue    = rgb8_from_int 0x0000FF;
        #
        rgb8_cyan    = rgb8_from_int 0x00FFFF;
        rgb8_magenta = rgb8_from_int 0xFF00FF;
        rgb8_yellow  = rgb8_from_int 0xFFFF00;
    };
end;

## COPYRIGHT (c) 1994 by AT&T Bell Laboratories
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext