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);
}
}
}
}

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.");
}
}

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");

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();

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

Further utility functions are:

Further Reading

maxon::Col4::b
T b
Definition: col4.h:35
Int
maxon::Int Int
Definition: ge_sys_math.h:64
ColorSwatchData
Definition: lib_colorchooser.h:405
AutoFree::Assign
void Assign(TYPE *p)
Definition: ge_autoptr.h:225
maxon::Col4::r
T r
Definition: col4.h:33
AutoFree
Definition: ge_autoptr.h:151
ColorSwatchGroup::GetColorCount
Int GetColorCount() const
ColorSwatchGroup::GetName
String GetName() const
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:66
maxon::Float
Float64 Float
Definition: apibase.h:195
ColorSwatchGroup::SetName
void SetName(String name)
DESCFLAGS_SET::NONE
@ NONE
None.
ColorSwatchData::GetGroupAtIndex
ColorSwatchGroup * GetGroupAtIndex(Int index, SWATCH_CATEGORY category=SWATCH_CATEGORY::DOCUMENT)
maxon::Col4
A color consisting of three components R, G, B and an alpha.
Definition: col4.h:14
BaseDocument::InsertMaterial
void InsertMaterial(BaseMaterial *mat, BaseMaterial *pred=nullptr, Bool checknames=false)
ColorSwatchGroup::SortColors
void SortColors()
Sorts colors in the group based in their HSV values.
C4DAtom::SetParameter
Bool SetParameter(const DescID &id, const GeData &t_data, DESCFLAGS_SET flags)
maxon::Vec3< maxon::Float64, 1 >
Material
Definition: c4d_basematerial.h:240
Material::Alloc
static Material * Alloc()
ColorSwatchGroup::IsGroupSelected
Bool IsGroupSelected() const
ColorSwatchData::Alloc
static ColorSwatchData * Alloc(BaseDocument *doc=nullptr, Bool global=false)
ColorSwatchGroup
Definition: lib_colorchooser.h:217
ApplicationOutput
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:208
maxon::Col4::g
T g
Definition: col4.h:34
ColorSwatchData::GetGroupCount
Int GetGroupCount(SWATCH_CATEGORY category=SWATCH_CATEGORY::DOCUMENT) const
Random::Get01
Float Get01(void)
ColorSwatchGroup::AddColor
Int AddColor(const maxon::ColorA &color, Bool selected=false, Int insertAt=-1)
Bool
maxon::Bool Bool
Definition: ge_sys_math.h:55
Random
Definition: c4d_tools.h:811
MATERIAL_COLOR_COLOR
@ MATERIAL_COLOR_COLOR
Definition: mmaterial.h:56
ColorSwatchData::AddGroup
ColorSwatchGroup * AddGroup(SWATCH_CATEGORY category=SWATCH_CATEGORY::DOCUMENT, const String &name=String(), Bool selected=false, Int insertAt=-1, const ColorAlphaArray &colors=ColorAlphaArray())
ColorSwatchGroup::GetColor
Bool GetColor(Int index, maxon::ColorA &color, Bool *selected=nullptr) const