Color Functions Manual

About

Along with the ColorSwatchData and ColorSwatchGroup types these utility functions were introduced to handle colors. The functions are defined in the lib_colorchooser.h header file.

String Conversion

These functions convert a given RGB color value to a formatted String:

// This example reads the color parameter of the given material
// and prints the value as RGB and HSV.
// access MATERIAL_COLOR_COLOR parameter
GeData data;
if (!material->GetParameter(ConstDescID(DescLevel(MATERIAL_COLOR_COLOR)), data, DESCFLAGS_GET::NONE))
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION);
const Vector color = data.GetVector();
const String rgbString = ColorRGBToString(color);
const String hsvString = ColorHSVToString(color);
ApplicationOutput("Material Color: RGB " + rgbString + ", HSV " + hsvString);
Definition: c4d_gedata.h:83
const Vector & GetVector() const
Definition: c4d_gedata.h:486
Definition: c4d_string.h:41
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:204
String ColorHSVToString(const Vector &color)
String ColorRGBToString(const Vector &color)
#define ConstDescID(...)
Definition: lib_description.h:594
@ MATERIAL_COLOR_COLOR
Definition: mmaterial.h:56
Represents a level within a DescID.
Definition: lib_description.h:298

Color Format Conversion

The following functions allow easy conversion of colors stored in different formats.

These functions convert float colors (0.0 ... 1.0) to 8-bit colors (0 .. 255):

These functions convert float colors (0.0 ... 1.0) to 16-bit colors (0 .. 65536):

Also, a Kelvin color temperature can be converted:

// This example calculates a RGB value from a light temperature
// and applies it to the given "Light" object.
const Float kelvin = 2600.0; // light bulb
const Vector rgb = ColorKelvinTemperatureToRGB(kelvin);
light->SetParameter(ConstDescID(DescLevel(LIGHT_COLOR)), rgb, DESCFLAGS_SET::NONE);
maxon::Float Float
Definition: ge_sys_math.h:62
Vector ColorKelvinTemperatureToRGB(Float kelvinDegrees, Float tint=0.0)
@ LIGHT_COLOR
Definition: olight.h:6

Color Harmony

These functions are used to calculate color palettes based on the given input color.

Note
The output maxon::BaseArray contains also the original color.
// This example reads the color value from the given material.
// The complementary color is calculated and applied to a new material.
// access MATERIAL_COLOR_COLOR parameter
GeData data;
if (!material->GetParameter(ConstDescID(DescLevel(MATERIAL_COLOR_COLOR)), data, DESCFLAGS_GET::NONE))
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION);
// the original color
const Vector color = data.GetVector();
// calculate the complementary color
if (!ColorHarmonyGetComplementary(color, false, results))
return maxon::UnknownError(MAXON_SOURCE_LOCATION);
// check if the result contains both the original and complementary color
if (results.GetCount() != 2)
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION);
// get the result color
const Vector complementaryColor = results[1];
// create new material with complementary color
if (newMaterial == nullptr)
return maxon::OutOfMemoryError(MAXON_SOURCE_LOCATION);
doc->InsertMaterial(newMaterial);
Definition: c4d_basematerial.h:28
static BaseMaterial * Alloc(Int32 type)
Bool SetParameter(const DescID &id, const GeData &t_data, DESCFLAGS_SET flags)
Definition: basearray.h:415
MAXON_ATTRIBUTE_FORCE_INLINE Int GetCount() const
Definition: basearray.h:585
#define Mmaterial
Standard material.
Definition: ge_prepass.h:1008
Bool ColorHarmonyGetComplementary(const Vector &color, Bool ryb, maxon::BaseArray< Vector > &palette)
const char * doc
Definition: pyerrors.h:226

Further Reading