2014-07-08 03:11:14 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Palette.h"
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// PaletteList class.
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
namespace EmberNs
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Holds a list of palettes read from an Xml file. Since the default list from flam3-palettes.xml is fairly large at 700 palettes,
|
|
|
|
/// the list member is kept as a static. This class derives from EmberReport in order to report any errors that occurred while reading the Xml.
|
|
|
|
/// Note that although the Xml color values are expected to be 0-255, they are converted and stored as normalized colors, with values from 0-1.
|
|
|
|
/// Template argument expected to be float or double.
|
|
|
|
/// </summary>
|
|
|
|
template <typename T>
|
|
|
|
class EMBER_API PaletteList : public EmberReport
|
|
|
|
{
|
|
|
|
public:
|
2015-04-08 21:23:29 -04:00
|
|
|
static const char* m_DefaultFilename;
|
|
|
|
|
2014-07-08 03:11:14 -04:00
|
|
|
/// <summary>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// Empty constructor which initializes the palette map with the default palette file.
|
2014-07-08 03:11:14 -04:00
|
|
|
/// </summary>
|
|
|
|
PaletteList()
|
|
|
|
{
|
2015-04-08 21:23:29 -04:00
|
|
|
Add(string(m_DefaultFilename));
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Read an Xml palette file into memory.
|
|
|
|
/// This must be called before any palette file usage.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="filename">The full path to the file to read</param>
|
|
|
|
/// <param name="force">If true, override the initialization state and force a read, else observe the initialization state.</param>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// <returns>Whether anything was read</returns>
|
|
|
|
bool Add(const string& filename, bool force = false)
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
2015-04-08 21:23:29 -04:00
|
|
|
bool added = false;
|
|
|
|
auto& palettes = m_Palettes[filename];
|
2014-07-08 03:11:14 -04:00
|
|
|
|
2015-04-08 21:23:29 -04:00
|
|
|
if (palettes.empty() || force)
|
|
|
|
{
|
2014-07-08 03:11:14 -04:00
|
|
|
string buf;
|
2015-04-08 21:23:29 -04:00
|
|
|
const char* loc = __FUNCTION__;
|
2014-07-08 03:11:14 -04:00
|
|
|
|
|
|
|
if (ReadFile(filename.c_str(), buf))
|
|
|
|
{
|
2014-12-07 02:51:44 -05:00
|
|
|
xmlDocPtr doc = xmlReadMemory(static_cast<const char*>(buf.data()), int(buf.size()), filename.c_str(), nullptr, XML_PARSE_NONET);
|
2014-07-08 03:11:14 -04:00
|
|
|
|
2014-09-10 01:41:26 -04:00
|
|
|
if (doc != nullptr)
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
|
|
|
xmlNode* rootNode = xmlDocGetRootElement(doc);
|
|
|
|
|
2015-04-08 21:23:29 -04:00
|
|
|
palettes.clear();
|
|
|
|
palettes.reserve(buf.size() / 2048);//Roughly what it takes per palette.
|
|
|
|
ParsePalettes(rootNode, palettes);
|
2014-07-08 03:11:14 -04:00
|
|
|
xmlFreeDoc(doc);
|
2015-04-08 21:23:29 -04:00
|
|
|
added = true;
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_ErrorReport.push_back(string(loc) + " : Couldn't load xml doc");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_ErrorReport.push_back(string(loc) + " : Couldn't read palette file " + filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 21:23:29 -04:00
|
|
|
return added;
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// Get the palette at a random index in a random file in the map.
|
2014-07-08 03:11:14 -04:00
|
|
|
/// </summary>
|
2015-04-08 21:23:29 -04:00
|
|
|
Palette<T>* GetRandomPalette()
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
2015-04-08 21:23:29 -04:00
|
|
|
auto p = m_Palettes.begin();
|
|
|
|
int i = 0, paletteFileIndex = QTIsaac<ISAAC_SIZE, ISAAC_INT>::GlobalRand->Rand() % Size();
|
|
|
|
|
|
|
|
while (i < paletteFileIndex && p != m_Palettes.end())
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < Size())
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
2015-04-08 21:23:29 -04:00
|
|
|
int paletteIndex = QTIsaac<ISAAC_SIZE, ISAAC_INT>::GlobalRand->Rand() % p->second.size();
|
|
|
|
|
|
|
|
if (paletteIndex < p->second.size())
|
|
|
|
return &p->second[paletteIndex];
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
|
|
|
|
2014-09-10 01:41:26 -04:00
|
|
|
return nullptr;
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
2014-09-10 01:41:26 -04:00
|
|
|
|
2014-07-08 03:11:14 -04:00
|
|
|
/// <summary>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// Get the palette at a specified index in the specified file in the map.
|
2014-07-08 03:11:14 -04:00
|
|
|
/// </summary>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// <param name="filename">The filename of the palette to retrieve</param>
|
|
|
|
/// <param name="i">The index of the palette to read. A value of -1 indicates a random palette.</param>
|
|
|
|
/// <returns>A pointer to the requested palette if the index was in range, else nullptr.</returns>
|
|
|
|
Palette<T>* GetPalette(const string& filename, int i)
|
|
|
|
{
|
|
|
|
auto& palettes = m_Palettes[filename];
|
|
|
|
|
|
|
|
if (!palettes.empty() && i < int(palettes.size()))
|
|
|
|
return &palettes[i];
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get a pointer to a palette with a specified name in the specified file in the map.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="filename">The filename of the palette to retrieve</param>
|
2014-07-08 03:11:14 -04:00
|
|
|
/// <param name="name">The name of the palette to retrieve</param>
|
2014-09-10 01:41:26 -04:00
|
|
|
/// <returns>A pointer to the palette if found, else nullptr</returns>
|
2015-04-08 21:23:29 -04:00
|
|
|
Palette<T>* GetPaletteByName(const string& filename, const string& name)
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
2015-04-08 21:23:29 -04:00
|
|
|
for (auto& palettes : m_Palettes)
|
|
|
|
if (palettes.first == filename)
|
|
|
|
for (auto& palette : palettes.second)
|
|
|
|
if (palette.m_Name == name)
|
|
|
|
return &palette;
|
2014-07-08 03:11:14 -04:00
|
|
|
|
2014-09-10 01:41:26 -04:00
|
|
|
return nullptr;
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// Get a copy of the palette at a specified index in the specified file in the map
|
|
|
|
/// with its hue adjusted by the specified amount.
|
2014-07-08 03:11:14 -04:00
|
|
|
/// </summary>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// <param name="filename">The filename of the palette to retrieve</param>
|
|
|
|
/// <param name="i">The index of the palette to read.</param>
|
2014-07-08 03:11:14 -04:00
|
|
|
/// <param name="hue">The hue adjustment to apply</param>
|
|
|
|
/// <param name="palette">The palette to store the output</param>
|
|
|
|
/// <returns>True if successful, else false.</returns>
|
2015-04-08 21:23:29 -04:00
|
|
|
bool GetHueAdjustedPalette(const string& filename, int i, T hue, Palette<T>& palette)
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
|
|
|
bool b = false;
|
|
|
|
|
2015-04-08 21:23:29 -04:00
|
|
|
if (Palette<T>* unadjustedPal = GetPalette(filename, i))
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
|
|
|
unadjustedPal->MakeHueAdjustedPalette(palette, hue);
|
|
|
|
b = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Clear the palette list and reset the initialization state.
|
|
|
|
/// </summary>
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
m_Palettes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// Get the size of the palettes map.
|
|
|
|
/// This will be the number of files read.
|
2014-07-08 03:11:14 -04:00
|
|
|
/// </summary>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// <returns>The size of the palettes map</returns>
|
2014-10-27 17:21:06 -04:00
|
|
|
size_t Size() { return m_Palettes.size(); }
|
2014-07-08 03:11:14 -04:00
|
|
|
|
2015-04-08 21:23:29 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Get the size of specified palette vector in the palettes map.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">The index of the palette in the map to retrieve</param>
|
|
|
|
/// <returns>The size of the palette vector at the specified index in the palettes map</returns>
|
|
|
|
size_t Size(size_t index)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
auto p = m_Palettes.begin();
|
|
|
|
|
|
|
|
while (i < index && p != m_Palettes.end())
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p->second.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get the size of specified palette vector in the palettes map.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="s">The filename of the palette in the map to retrieve</param>
|
|
|
|
/// <returns>The size of the palette vector at the specified index in the palettes map</returns>
|
|
|
|
size_t Size(const string& s)
|
|
|
|
{
|
|
|
|
return m_Palettes[s].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get the name of specified palette in the palettes map.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index">The index of the palette in the map to retrieve</param>
|
|
|
|
/// <returns>The name of the palette vector at the specified index in the palettes map</returns>
|
|
|
|
const string& Name(size_t index)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
auto p = m_Palettes.begin();
|
|
|
|
|
|
|
|
while (i < index && p != m_Palettes.end())
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p->first;
|
|
|
|
}
|
|
|
|
|
2014-07-08 03:11:14 -04:00
|
|
|
private:
|
|
|
|
/// <summary>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// Parses an Xml node for all palettes present and store in the passed in palette vector.
|
2014-07-08 03:11:14 -04:00
|
|
|
/// Note that although the Xml color values are expected to be 0-255, they are converted and
|
|
|
|
/// stored as normalized colors, with values from 0-1.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="node">The parent note of all palettes in the Xml file.</param>
|
2015-04-08 21:23:29 -04:00
|
|
|
/// <param name="palettes">The vector to store the paresed palettes associated with this file in.</param>
|
|
|
|
void ParsePalettes(xmlNode* node, vector<Palette<T>>& palettes)
|
2014-07-08 03:11:14 -04:00
|
|
|
{
|
|
|
|
bool hexError = false;
|
|
|
|
char* val;
|
|
|
|
const char* loc = __FUNCTION__;
|
|
|
|
xmlAttrPtr attr;
|
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
if (node->type == XML_ELEMENT_NODE && !Compare(node->name, "palette"))
|
|
|
|
{
|
|
|
|
attr = node->properties;
|
|
|
|
Palette<T> palette;
|
|
|
|
|
|
|
|
while (attr)
|
|
|
|
{
|
2014-12-07 02:51:44 -05:00
|
|
|
val = reinterpret_cast<char*>(xmlGetProp(node, attr->name));
|
2014-07-08 03:11:14 -04:00
|
|
|
|
|
|
|
if (!Compare(attr->name, "data"))
|
|
|
|
{
|
|
|
|
int colorIndex = 0;
|
2014-12-06 00:05:09 -05:00
|
|
|
uint r, g, b;
|
2014-07-08 03:11:14 -04:00
|
|
|
int colorCount = 0;
|
|
|
|
hexError = false;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2014-12-07 02:51:44 -05:00
|
|
|
int ret = sscanf_s(static_cast<char*>(&(val[colorIndex])),"00%2x%2x%2x", &r, &g, &b);
|
2014-07-08 03:11:14 -04:00
|
|
|
|
|
|
|
if (ret != 3)
|
|
|
|
{
|
|
|
|
m_ErrorReport.push_back(string(loc) + " : Problem reading hexadecimal color data " + string(&val[colorIndex]));
|
|
|
|
hexError = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
colorIndex += 8;
|
|
|
|
|
2014-12-07 02:51:44 -05:00
|
|
|
while (isspace(int(val[colorIndex])))
|
2014-07-08 03:11:14 -04:00
|
|
|
colorIndex++;
|
|
|
|
|
|
|
|
palette[colorCount].r = T(r) / T(255);//Store as normalized colors in the range of 0-1.
|
|
|
|
palette[colorCount].g = T(g) / T(255);
|
|
|
|
palette[colorCount].b = T(b) / T(255);
|
|
|
|
|
|
|
|
colorCount++;
|
|
|
|
} while (colorCount < COLORMAP_LENGTH);
|
|
|
|
}
|
|
|
|
else if (!Compare(attr->name, "number"))
|
|
|
|
{
|
|
|
|
palette.m_Index = atoi(val);
|
|
|
|
}
|
|
|
|
else if (!Compare(attr->name, "name"))
|
|
|
|
{
|
|
|
|
palette.m_Name = string(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlFree(val);
|
|
|
|
attr = attr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hexError)
|
|
|
|
{
|
2015-04-08 21:23:29 -04:00
|
|
|
palettes.push_back(palette);
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-08 21:23:29 -04:00
|
|
|
ParsePalettes(node->children, palettes);
|
2014-07-08 03:11:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
node = node->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 21:23:29 -04:00
|
|
|
static map<string, vector<Palette<T>>> m_Palettes;//The map of filenames to vectors that store the palettes.
|
2014-07-08 03:11:14 -04:00
|
|
|
};
|
2014-09-10 01:41:26 -04:00
|
|
|
}
|