void ColorPalettes::addColorPalette(ColorPalette &newPalette, String const &name) { // Do we already own this palette? if (d->colorPalettes.contains(newPalette.id())) return; d->colorPalettes.insert(newPalette.id(), &newPalette); if (!name.isEmpty()) { d->colorPaletteNames.insert(name, &newPalette); } // If this is the first palette automatically set it as the default. if (d->colorPalettes.count() == 1) { d->defaultColorPalette = newPalette.id(); } DENG2_FOR_AUDIENCE2(Addition, i) i->colorPaletteAdded(newPalette); }
DENG_EXTERN_C colorpaletteid_t R_CreateColorPalette(char const *colorFormatDescriptor, char const *nameCStr, uint8_t const *colorData, int colorCount) { DENG2_ASSERT(nameCStr != 0 && colorFormatDescriptor != 0 && colorData != 0); LOG_AS("R_CreateColorPalette"); try { using namespace res; String name(nameCStr); if(name.isEmpty()) { LOG_RES_WARNING("Invalid/zero-length name specified, ignoring."); return 0; } QVector<Vector3ub> colors = ColorTableReader::read(colorFormatDescriptor, colorCount, colorData); auto &palettes = Resources::get().colorPalettes(); // Replacing an existing palette? if(palettes.hasColorPalette(name)) { ColorPalette &palette = palettes.colorPalette(name); palette.replaceColorTable(colors); return palette.id(); } // A new palette. ColorPalette *palette = new ColorPalette(colors); palettes.addColorPalette(*palette, name); return palette->id(); } catch(res::ColorTableReader::FormatError const &er) { LOG_RES_WARNING("Error creating/replacing color palette '%s':\n") << nameCStr << er.asText(); } return 0; }