About

A ColorSwatchGroup stores multiple colors as maxon::ColorA. The group itself and the colors can be selected. ColorSwatchGroup elements are stored and handled using ColorSwatchData objects. The class is defined in the lib_colorchooser.h header file.

// This example loops through all colors of all color groups of the given BaseDocument.
// For each selected color a new material is created.
// load document color swatches
ColorSwatchData* const colorSwatchData = ColorSwatchData::Alloc(doc, false);
if (colorSwatchData == nullptr)
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION);
// AutoFree takes ownership
freeData.Assign(colorSwatchData);
// loop through groups
const Int groupCount = colorSwatchData->GetGroupCount();
for (Int i = 0; i < groupCount; ++i)
{
ColorSwatchGroup* group = colorSwatchData->GetGroupAtIndex(i);
if (group)
{
// loop through colors
const Int colorCount = group->GetColorCount();
for (Int c = 0; c < colorCount; ++c)
{
Bool selected = false;
// get color and check if it is selected
if (group->GetColor(c, color, &selected) && selected)
{
// create new material
Material* const mat = Material::Alloc();
if (mat == nullptr)
return maxon::OutOfMemoryError(MAXON_SOURCE_LOCATION);
// use color
const Vector rgb { color.r, color.g, color.b };
doc->InsertMaterial(mat);
}
}
}
}
Py_ssize_t i
Definition: abstract.h:645
Definition: ge_autoptr.h:155
void Assign(TYPE *p)
Definition: ge_autoptr.h:234
Bool SetParameter(const DescID &id, const GeData &t_data, DESCFLAGS_SET flags)
Definition: lib_colorchooser.h:406
Int GetGroupCount(SWATCH_CATEGORY category=SWATCH_CATEGORY::DOCUMENT) const
ColorSwatchGroup * GetGroupAtIndex(Int index, SWATCH_CATEGORY category=SWATCH_CATEGORY::DOCUMENT)
static ColorSwatchData * Alloc(BaseDocument *doc=nullptr, Bool global=false)
Definition: lib_colorchooser.h:218
Bool GetColor(Int index, maxon::ColorA &color, Bool *selected=nullptr) const
Int GetColorCount() const
Definition: c4d_basematerial.h:241
static Material * Alloc()
Py_UNICODE c
Definition: unicodeobject.h:1200
maxon::Bool Bool
Definition: ge_sys_math.h:55
maxon::Int Int
Definition: ge_sys_math.h:64
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67
#define ConstDescID(...)
Definition: lib_description.h:594
@ MATERIAL_COLOR_COLOR
Definition: mmaterial.h:56
const char * doc
Definition: pyerrors.h:226
Represents a level within a DescID.
Definition: lib_description.h:298
A color consisting of three components R, G, B and an alpha.
Definition: col4.h:16
T r
Definition: col4.h:35
T g
Definition: col4.h:36
T b
Definition: col4.h:37

Allocation/Deallocation

ColorSwatchGroups can be created using the usual tools. This is typically not needed.

Access

ColorSwatchGroup elements are stored in a ColorSwatchData object. See ColorSwatchData Color Groups

Selection

A ColorSwatchGroup can be selected:

// This example loops through all groups and prints the names of the selected groups.
const Int groupCount = colorSwatchData->GetGroupCount();
for (Int i = 0; i < groupCount; ++i)
{
const ColorSwatchGroup* const group = colorSwatchData->GetGroupAtIndex(i);
if (group == nullptr)
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION);
// check if a group could be accessed
// and if that group is selected
if (group->IsGroupSelected())
{
ApplicationOutput("Group " + group->GetName() + " is selected.");
}
}
String GetName() const
Bool IsGroupSelected() const
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:210

Name

A ColorSwatchGroup can be identified by a name:

// This example adds a new color group.
ColorSwatchGroup* const group = colorSwatchData->AddGroup();
if (group == nullptr)
return maxon::OutOfMemoryError(MAXON_SOURCE_LOCATION);
group->SetName("This is a new group");
ColorSwatchGroup * AddGroup(SWATCH_CATEGORY category=SWATCH_CATEGORY::DOCUMENT, const String &name=String(), Bool selected=false, Int insertAt=-1, const ColorAlphaArray &colors=ColorAlphaArray())
void SetName(String name)

Colors

A ColorSwatchGroup stores multiple colors and their selection state. The colors are obtained with:

// This example prints the values of all colors of the given group.
const Int colorCount = group->GetColorCount();
for (Int c = 0; c < colorCount; ++c)
{
// access the color with the index 'c'
if (!group->GetColor(c, color))
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION);
ApplicationOutput("Color: @", color);
}

Colors can be edited with:

New colors are added with:

// This example adds ten random colors to the given group.
Random random;
for (Int c = 0; c < 10; ++c)
{
const maxon::Float r = random.Get01();
const maxon::Float g = random.Get01();
const maxon::Float b = random.Get01();
const maxon::ColorA color(r, g, b, 1.0);
group->AddColor(color, false);
}
group->SortColors();
void SortColors()
Sorts colors in the group based in their HSV values.
Int AddColor(const maxon::ColorA &color, Bool selected=false, Int insertAt=-1)
Definition: c4d_tools.h:835
MAXON_ATTRIBUTE_FORCE_INLINE Float Get01()
Definition: c4d_tools.h:865
Float64 Float
Definition: apibase.h:222
const char const char grammar * g
Definition: parsetok.h:52

The selection status of a color can be utilized with these functions:

Further utility functions are:

Further Reading