ColorSwatchData Manual
A
ColorSwatchData
object can manage multiple color groups. These color groups can be loaded globally from
Cinema 4D
,
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.
// load document color swatches ColorSwatchData * const colorSwatchData = ColorSwatchData::Alloc (doc, false ); if (colorSwatchData == nullptr ) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION );
// AutoFree takes ownership AutoFree<ColorSwatchData> freeData; freeData. 赋值 (colorSwatchData);
// loop through groups const Int groupCount = colorSwatchData-> GetGroupCount ();
// get color and check if it is selected if (group-> GetColor (c, color, &selected) && selected) { // create new material 材质 * const mat = Material::Alloc (); if (mat == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );
// use color const 向量 rgb { color. r , color. g , color. b }; mat-> SetParameter ( MATERIAL_COLOR_COLOR , rgb, DESCFLAGS_SET::NONE ); doc-> InsertMaterial (mat); } } } }
ColorSwatchData instances are created with the usual tools:
Color groups can be stored in a BaseDocument :
// 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);
Color swatches can also be stored in the preset library:
To make work with presets easier these utility functions exist:
// check if preset already exists if (! ColorSwatchData::PresetExists (presetName)) { // construct url SDKBrowserURL presetUrl = ColorSwatchData::GetPresetDirectory (); presetUrl += presetName;
// save preset if (!colorSwatchData-> SavePreset (presetUrl, "User" , "This is my preset" )) return maxon::UnknownError( MAXON_SOURCE_LOCATION ); }
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 ).
Further utility functions are: