bx-lib : the Basic X11 Library tuned for C beginners  1.0
Functions
Functions manipulating the RGB model
Color (the main attribute of the pen)

Functions

bx_color bx_rgb_to_color (unsigned char r, unsigned char g, unsigned char b)
bx_color bx_mean_color (bx_color c1, bx_color c2)
bx_color bx_blend_color (bx_color c1, double alpha, bx_color c2)

Detailed Description

Function Documentation

bx_color bx_rgb_to_color ( unsigned char  r,
unsigned char  g,
unsigned char  b 
)

Returns the color suported by the system that best approximates a given RGB (Red,Green,Blue) triplet. The three channels are in the range from 0 to 255 (or from 0x00 to 0xFF in hexadecimal base).

Parameters
rthe red channel, in the range from 0x00 to 0xFF (saturated).
gthe green channel, in the range from 0x00 to 0xFF(saturated).
bthe blue channel, in the range from 0x00 to 0xFF (saturated).
Returns
the best matching color for the (r,g,b) triplet that is supported by the system.
Remarks
  • currently, only TrueColor and PseudoColor video modes are supported.
  • in TrueColor, the bit-pattern of a pixel value directly encodes the (R,G,B) triplet of its color. The most common encodings are 24 bits per pixel (= 8+8+8 bits, that is a 256*256*256 RGB cube) and 16 bits per pixel (= 5+6+5 bits, that is a 32*64*32 RGB cube) with the green channel having one more bit than red and blue).
  • in PseudoColor, pixel values are 8-bits values that are indexes in a pre-allocated 256 colors palette. The bx-lib pre-allocates a palette such that you have an 8*8*4 "poor-man" RGB cube stored in it, making any bit-pattern of index a kind of degenerate TrueColor encoding with 8 bits per pixels (= 3+3+2 bits, the blue channel having 1 bit less than red and green).
  • (0xFF,0x00,0x00), (0x00,0xFF,0x00), and (0x00,0x00,0xFF) respetively yield red, green and blue.
  • (0xFF,0xFF,0x00), (0xFF,0x00,0xFF), and (0x00,0xFF,0xFF) respectively yield yellow, magenta and cyan.
  • (0x00,0x00,0x00), (0x80,0x80,0x80) and (0xFF,0xFF,0xFF) respectively yield black, gray and white.
    bx_rgb_to_color.jpg
    #include <bx.h>
    int main (void)
    {
    bx_window win;
    bx_color c1, c2, c3;
    int w= 180, h= 60;
    bx_init ();
    win= bx_create_window ("bx_rgb_to_color", 10 , 10, w, h);
    c1= bx_rgb_to_color (0x00, 0xFF, 0xFF);
    c2= bx_rgb_to_color (0xFF, 0x00, 0xFF);
    c3= bx_rgb_to_color (0xFF, 0xFF, 0x00);
    bx_set_color (c1); bx_draw_box (win, 0*w/3, 0, w/3, h, BX_FILLED);
    bx_set_color (c2); bx_draw_box (win, 1*w/3, 0, w/3, h, BX_FILLED);
    bx_set_color (c3); bx_draw_box (win, 2*w/3, 0, w/3, h, BX_FILLED);
    bx_show_canvas (win, 0);
    return 0;
    }
bx_color bx_mean_color ( bx_color  c1,
bx_color  c2 
)

Computes an approximation of the color c averaging two colors c1 and c2, that is if c1=(r1,g1,b1) and c2=(r2,g2,b2) as (Red,Green,Blue) triplets, then c=((r1+r2)/2, (g1+g2)/2,(b1+b2)/2).

Parameters
c1the first color.
c2the second color.
Returns
the mean color c of c1 and c2.
bx_mean_color.jpg
#include <bx.h>
int main (void)
{
bx_window win;
bx_color c1, c2, c3;
int w= 180, h= 60;
bx_init ();
win= bx_create_window ("bx_mean_color", 10 , 10, w, h);
c1= bx_pink (); c3= bx_orange ();
c2= bx_mean_color (c1, c3);
bx_set_color (c1); bx_draw_box (win, 0*w/3, 0, w/3, h, BX_FILLED);
bx_set_color (c2); bx_draw_box (win, 1*w/3, 0, w/3, h, BX_FILLED);
bx_set_color (c3); bx_draw_box (win, 2*w/3, 0, w/3, h, BX_FILLED);
bx_show_canvas (win, 0);
return 0;
}
bx_color bx_blend_color ( bx_color  c1,
double  alpha,
bx_color  c2 
)

Computes an approximation of the color c of an alpha-weighted blend of two colors c1 and c2, that is if c1=(r1,g1,b1) and c2=(r2,g2,b2) as (Red,Green,Blue) triplets, then c=(r1+(r2-r1)*alpha, g1+(g2-g1)*alpha, b1+(b2-b1)*alpha) for some given alpha in the range (0.0, 1.0).

Parameters
c1the first color.
c2the second color.
alphaa blending coefficient in the range (0.0, 1.0).
Returns
the blend of c1 and c2 by coefficient alpha.
Remarks