cdEncodeColor
| Definition: |
include pGUI.e
integer colour = cdEncodeColor(atom red, green, blue) -- or -- atom colour = cdEncodeColorAlpha(atom red, green, blue, alpha) -- or -- atom colour = cdEncodeAlpha(atom colour, integer alpha) -- or -- atom colour = rgb(atom red, green, blue, alpha=0) -- or -- atom colour = hsv_to_rgb(atom h, s, v, a=0) -- and -- integer {red,green,blue} = cdDecodeColor(atom colour) -- or -- integer {red,green,blue,alpha} = cdDecodeColorAlpha(atom colour) -- or -- integer alpha = cdDecodeAlpha(atom colour) -- or -- integer {red,green,blue,alpha} = to_rgb(atom colour) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Description: |
Various utility routines to convert between colour components and their combined equivalent encodings.
red, green, blue, alpha: Notionally integers 0..255 but fractions permitted (see comment). colour: an encoding in the format 0xAARRGGBB. h,s,v,a: fractions between 0 and 1 (hue, saturation, value, and alpha). |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pwa/p2js: | Supported. Note that under pwa/p2js both cdDecodeColor() and cdDecodeColorAlpha() are simple aliases of to_rgb(), and therefore the first returns a quadlet rather than a triplet, unlike desktop/Phix. Obviously that could easily be rectified, but if/when genuinely needed and not before. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comments: |
Used in the CD library to define colors. Can be used without an active canvas.
Technically, and of little practical concern, except perhaps when testing, the precise nature of any fraction ==>> 0..255 conversion is officially undefined, especially for values below 0 or above 255.49, and may differ between different operating systems, libc/msvcrt builds, and even CPU brands. For consistent results, should that be vitally important, explicitly round/truncate any such fractions yourself, and for fairly obvious reasons hsv_to_rgb() is unlikely to be perfectly consistent between 32 and 64 bit (pretty much no matter what you do). The cdEncodeColor() and cdEncodeColorAlpha() routines invoke the C functions of the same name, whereas the rgb() function performs the same task in pure Phix code. The latter is recommended (simply have/omit the fourth parameter as needed), and is more likely to behave consistently across different platforms. A very similar rgb() routine exists in both arwen and win32lib, note however those libraries encode colours as red+green*#100+blue*#10000, whereas pGUI uses red*#10000+green*#100+blue, a fact you should only ever need to know when debugging or for some reason are directly comparing the results or using hard-coded literals such as #FF0000 (==CD_RED in pGUI) vs #0000FF (==BrightRed in arwen), which you may encounter when porting existing code. The cdDecodeColor(), cdDecodeColorAlpha(), and to_rgb() routines reverse the process, again with the latter being pure Phix and usually recommended: Just code integer {r,g,b} = to_rgb(), or maybe integer {r,g,b,{}} = to_rgb() if you don’t need the alpha value, however if you find yourself having to explicitly chop off the returned a, then using cdDecodeColour() may in fact be the better choice. Unlike the encode functions, no inconsistencies have ever been spotted between any of the decode routines. cdEncodeAlpha returns the given color coded with the alpha information. ATTENTION: At the moment only the Cairo, GDI+, XRender and IMAGERGB drivers support alpha components in color coding. The internal representation of the component is inverted, because the default value must be 0 and opaque for backward compatibility, so you should use the cdDecodeAlpha function or the cdAlpha macro to retrieve the alpha component. 0 is transparent, 255 is opaque. cdDecodeAlpha returns the alpha component of a color in the CD library. Can be used without an active canvas. 0 is transparent, 255 is opaque. The following constants are defined for convenience.
|