About

A ColorSwatchData object can manage multiple color groups. These color groups can be loaded globally from Cinema 4D, a BaseDocument or a preset and saved back to these sources. Local color groups and global color groups are stored separately. 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

ColorSwatchData instances are created with the usual tools:

// This example allocates a new ColorSwatchData object.
// During allocation the color groups of the given BaseDocument are automatically loaded.
if (data == nullptr)
return maxon::OutOfMemoryError(MAXON_SOURCE_LOCATION);
const Int groupCount = data->GetGroupCount();
ApplicationOutput("The document contains " + String::IntToString(groupCount) + " color groups");

Document Colors

Color groups can be stored in a BaseDocument:

// This example loads a Cinema 4D scene file.
// The color swatches of this file are loaded into the given ColorSwatchData object.
// The merged colors are saved back to the original document.
// load colors of the original
if (!colorSwatchData->Load(doc))
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION);
// select the scene file
Filename sceneFile;
// open a dialog to select a file
if (!sceneFile.FileSelect(FILESELECTTYPE::SCENES, FILESELECT::LOAD, "Select File"_s))
return maxon::OK;
// load the scene file
BaseDocument* loadedDoc = LoadDocument(sceneFile, SCENEFILTER::NONE, nullptr);
if (loadedDoc == nullptr)
return maxon::IoError(MAXON_SOURCE_LOCATION, MaxonConvert(sceneFile, MAXONCONVERTMODE::NONE), "Could not load document."_s);
// merge the scene file colors
colorSwatchData->Load(loadedDoc, true);
// store merged colors back into the BaseDocument
colorSwatchData->Save(doc);
BaseDocument::Free(loadedDoc);

Presets

Color swatches can also be stored in the preset library:

To make work with presets easier these utility functions exist:

// This example checks if a color swatches preset with the given name already exists.
// If not, the preset is created.
const String presetName("myColorPreset");
// check if preset already exists
{
// construct url
presetUrl += presetName;
// save preset
if (!colorSwatchData->SavePreset(presetUrl, "User", "This is my preset"))
return maxon::UnknownError(MAXON_SOURCE_LOCATION);
}

Color Groups

The color swatches stored in a ColorSwatchData are stored in groups. These groups can either be stored with the document (SWATCH_CATEGORY::DOCUMENT) or with Cinema 4D itself (SWATCH_CATEGORY::GLOBAL).

// This example adds a new group to the given ColorSwatchData.
// Then the name of each group is printed.
colorSwatchData->AddGroup(SWATCH_CATEGORY::DOCUMENT, "New Group");
const Int groupCount = colorSwatchData->GetGroupCount(SWATCH_CATEGORY::DOCUMENT);
for (Int i = 0; i < groupCount; ++i)
{
const ColorSwatchGroup* const group = colorSwatchData->GetGroupAtIndex(i, SWATCH_CATEGORY::DOCUMENT);
if (group != nullptr)
{
ApplicationOutput("Group Name: " + group->GetName());
}
}

Further utility functions are:

Further Reading

maxon::Col4::b
T b
Definition: col4.h:35
Int
maxon::Int Int
Definition: ge_sys_math.h:62
ColorSwatchData::SavePreset
Bool SavePreset(const String &name, const String &author=String(), const String &info=String(), Bool forceOverwrite=false)
ColorSwatchData
Definition: lib_colorchooser.h:405
FILESELECTTYPE::SCENES
@ SCENES
3D scene files.
ColorSwatchData::Free
static void Free(ColorSwatchData *&p)
AutoFree::Assign
void Assign(TYPE *p)
Definition: ge_autoptr.h:225
maxon::Col4::r
T r
Definition: col4.h:33
ColorSwatchData::Load
Bool Load(BaseDocument *doc, Bool merge=false, Bool loadGlobalColors=false)
SDKBrowserURL
Definition: lib_browser.h:70
SCENEFILTER::NONE
@ NONE
None.
Filename
Manages file and path names.
Definition: c4d_file.h:93
maxon::OK
return OK
Definition: apibase.h:2532
AutoFree
Definition: ge_autoptr.h:151
LoadDocument
BaseDocument * LoadDocument(const Filename &name, SCENEFILTER loadflags, BaseThread *thread, maxon::String *errorString=nullptr)
ColorSwatchGroup::GetColorCount
Int GetColorCount() const
ColorSwatchGroup::GetName
String GetName() const
MAXONCONVERTMODE::NONE
@ NONE
No check if file exists under case-sensitive drives.
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:66
DESCFLAGS_SET::NONE
@ NONE
None.
ColorSwatchData::GetGroupAtIndex
ColorSwatchGroup * GetGroupAtIndex(Int index, SWATCH_CATEGORY category=SWATCH_CATEGORY::DOCUMENT)
MaxonConvert
maxon::Url MaxonConvert(const Filename &fn, MAXONCONVERTMODE convertMode)
String
Definition: c4d_string.h:38
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)
String::IntToString
static String IntToString(Int32 v)
Definition: c4d_string.h:495
ColorSwatchData::Save
Bool Save(BaseDocument *doc, Bool saveGlobalColors=false)
C4DAtom::SetParameter
Bool SetParameter(const DescID &id, const GeData &t_data, DESCFLAGS_SET flags)
ColorSwatchData::PresetExists
static Bool PresetExists(const String &name, maxon::BaseArray< SDKBrowserURL > *urls=nullptr)
maxon::Vec3< maxon::Float64, 1 >
Material
Definition: c4d_basematerial.h:240
Material::Alloc
static Material * Alloc()
ColorSwatchData::Alloc
static ColorSwatchData * Alloc(BaseDocument *doc=nullptr, Bool global=false)
ColorSwatchGroup
Definition: lib_colorchooser.h:217
ApplicationOutput
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:207
BaseDocument::Free
static void Free(BaseDocument *&bl)
maxon::Col4::g
T g
Definition: col4.h:34
FILESELECT::LOAD
@ LOAD
Load dialog.
ColorSwatchData::GetGroupCount
Int GetGroupCount(SWATCH_CATEGORY category=SWATCH_CATEGORY::DOCUMENT) const
Bool
maxon::Bool Bool
Definition: ge_sys_math.h:53
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())
ColorSwatchData::GetPresetDirectory
static SDKBrowserURL GetPresetDirectory()
BaseDocument
Definition: c4d_basedocument.h:490
ColorSwatchGroup::GetColor
Bool GetColor(Int index, maxon::ColorA &color, Bool *selected=nullptr) const
Filename::FileSelect
Bool FileSelect(FILESELECTTYPE type, FILESELECT flags, const maxon::String &title, const maxon::String &force_suffix=maxon::String())
SWATCH_CATEGORY::DOCUMENT
@ DOCUMENT