Version 0.60.0

Colors

The color data type

The color data type is used to store general colors. trCAD supports two different color models: RGB and CMYK. Therefore, two distinct constructors for the color data type can be used.

Color constructors

The constructor for using the RGB color mode has the following signature:

rgb( int, int, int )

The three integral values represent the color channels red, green and blue and should be in the interval range [0,255]. The following script creates a color using the rgb constructor:

Example

color c = rgb( 255, 117, 56 )

The constructor for CMYK values has the form

cmyk( float, float, float, float )

The four arguments are the values of the cyan, magenta, yellow and key (black) channels that should be in the range [0.0, 1.0]:

Example

color c = cmyk( 0.63, 0.0, 0.96, 0.01 )

Channel access

In some cases it might be useful to access the channel value of a given color variable directly. This can be done by calling any of the data type members r, g, b, c, m, y or k:

Example

color c = rgb( 255, 117, 56 )
echo( c.g  )

Output

117

It is also possible to access values of a different color model than the one used in construction. The color model values are then converted automatically:

Example

color c = cmyk( 0.63, 0.0, 0.96, 0.01 )
echo( c.r  )

Output

94

Note

The trCAD color data type is not designed for considering different color spaces (https://en.wikipedia.org/wiki/RGB_color_space ) but mainly to provide a buffer for the numerical values of the respective color model. Of course the user can assign the RGB or CMYK data to a certain color space for his own by just proclaiming so.

For the same reason, the convertion between RGB and CMYK color models uses a simple one-to-one mapping algorithm that is very commonly used in web and computer graphics but that does not provide 100 percent convertion correctness. Users that rely on exact CMYK colors should only use the cmyk constructor during the complete process.

The color assignment modifier

The color assignment modifier is a modifier that paints a whole solid in one color. It is automatically constructed when the color data type is used in a modifier chain:

Example

make cmyk( 0.63, 0.0, 0.96, 0.01 ) >> sphere()
make rgb( 255, 0, 128 ) >> translation( <[1.2,0.0,0.0]>) >> sphere()

Output of the code example.

See also

🗙

Search results for