Color Values

Value > Basic Data Values > Color Values

 

   

Values and Collections - Quick Navigation

All the places that colors turn up in MAXScript (such as, wireColor, light color, and others) are represented by instances of the Color class. You can also use Point3 values to specify colors.

Literals

 red green blue white black orange yellow brown gray 

Predefined MAXScript globals.

Constructors

color <r> <g> <b> [ <a> ]

r,g,b and optional alpha are float values, typically in the range of 0 to 255.

<point3> as color

Interprets x as r, y as g, z as b. Alpha is assumed opaque (255).

Properties

<color>.red or .r: Float
<color>.green or .g: Float
<color>.blue or .b: Float
<color>.alpha or .a: Float

Color component properties

<color>.hue or .h: Float
<color>.saturation or .s: Float
<color>.value or .v: Float

Derived properties

Operators

<color> == <color>
<color> != <color> 
<color> + <color_or_number>
<color> - <color_or_number>
<color> * <color_or_number>
<color> / <color_or_number>

Channel-by-channel mathematical operations. Numbers operate on R,G,B and A channels equally.

-<color>

unary minus

IMPORTANT NOTES ON COLOR OPERATIONS:

The addition and subtraction operations are performed on the R, G, B and A channels equally:

aColor = color 1 1 1
(color111) aColor + aColor
(color 2 2 2 510)

The high Alpha value is caused by the fact that MAXScript does not print the Alpha value if it is 255, but internally the value of the variable aColor is (color 1 1 1 255). Thus, adding the Alpha channels of 255 to 255 produces an Alpha of 512, analogous to the R,G, and B channels.

While the Color Values allow floating point components, when originally implemented in3ds Max 2, it was assumed that they would be used mainly for 8 bit per channel RGBA values because MAXScript did not support floating point color bitmaps at that point in time. An internal conversion is performed when printing color values or converting other values to color.

Thus, multiplication of two color values leads to seemingly strange results:

aColor = color 1 1 1
aColor * aColor
(color 0.00392157 0.00392157 0.00392157)
aColor * [1,1,1]
(color 0.00392157 0.00392157 0.00392157)
aColor = color 128 128 128
aColor * aColor
(color 64.251 64.251 64.251)

The reason for these results when multiplying color values is that the actual component values are float values normally in the range of 0 to 1.

The scaling from 0-1 to 0-255 is an operation that MAXScript performs to the underlying color value when the value is printed or converted to some other value type.

A gray value of 128 multiplied by a gray value of 128 is not really 128*128 = 16384, which is a blown out white, but

(128/255.0 * 128/255.0)*255 or (.501961*.501961)*255

which is the DARKER shade of gray (color 64.251 64.251 64.251)

So, when (color 1 1 1 255) is converted to components in the range from 0 to 1, it also results in RGB components of (1.0/255*1.0/255)*255=0.00392157, while the Alpha of 255 turns into 1.0 and then back into 255.

Thus, the Color Value is not well suited for High Dynamic Range Image calculations with floating point color components.

For that type of operations, MAXScript provides a Point4 value that can be used to represent floating point colors without implicit conversion from/to the 0-255 range. This value was introduced in 3ds Max 6 to support the color parameters of mental ray shaders.

Methods

copy <color>

Creates a new copy of the color value.

FOR EXAMPLE:

newClr = copy oldClr

The new value contains a copy of the input color value, and is independent of the input color value.

random <from_color> <to_color>

Returns a random color between the given colors. The alpha component of the result will always be 255.

composite <color1> <color2>

Composites color1 over color2 through color1 's alpha. This function is equivalent to:

FOR EXAMPLE:

color1 + color2*((255. - color1.alpha)/255.)

3D Noise Functions

noise3 <color>
noise4 <color> <phase_float>
turbulence <color> <frequency_float>
fractalNoise <color> <H_float> <lacunarity_float> <octaves_float>

See Point3 Values for a description of the noise functions.

NOTE:

The component values of a Color value (r, g, b, and a) are normally in the range of 0.0 to 255.0. However, values outside this range are permissible. When color values outside the normal range are used in 3ds Max, 3ds Max clamps the values to the range of 0.0 to 255.0.

The following script shows the use of various literals, constructors, properties, operators, and methods of the Color class.

SCRIPT

-- color test bed
magenta=color 255 255 0 255-- create colors using constructors
aqua = [0, 255, 255] as color
aqua.v /= 2.-- reduce "strength" of aqua color
aqua
aqua.alpha=128-- set aqua to 50% opacity
aqua
lightGray=white/4-- create light gray color by dividing
-- each component of white by 4
composite aqua lightGray-- composite light gray over aqua
random black white-- generate a random color

OUTPUT:

(color 255 255 0)-- result of line 2. Note that if
-- alpha=255 it is not displayed
(color 0 255 255)-- result of line 3
127.5-- result of line 4 - value property of aqua
(color 0 127.5 127.5)-- result of line 5 - color of aqua
128-- result of line 6 - alpha property of aqua
(color 0 127.5 127.5 128)-- result of line 7 - color of aqua
(color 63.75 63.75 63.75 63.75)-- result of line 8 - each component divided by 4
(color 31.75 159.25 159.25 159.75)-- result of line 10
(color 249.607 27.8243 140.23)-- result of line 11