diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-02-27 23:56:03 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-02-27 23:56:03 +0000 |
commit | 5c703a1549b88339ca48ac3c48f67ab7503d223a (patch) | |
tree | d56cb2b44002c14ea0dcc57605a83895b2ff1da8 | |
parent | Load the X11 RGB colour definitions into a map (diff) | |
download | ilt-5c703a1549b88339ca48ac3c48f67ab7503d223a.tar.bz2 ilt-5c703a1549b88339ca48ac3c48f67ab7503d223a.tar.xz ilt-5c703a1549b88339ca48ac3c48f67ab7503d223a.zip |
Support for named colours in assets
Fixes up some error handling in colour parser.
-rw-r--r-- | assetFactory/style.cpp | 14 | ||||
-rw-r--r-- | res/brush47.xml | 6 |
2 files changed, 15 insertions, 5 deletions
diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp index 099d81a..d2977a7 100644 --- a/assetFactory/style.cpp +++ b/assetFactory/style.cpp @@ -1,12 +1,16 @@ #include "style.h" +#include "assetFactory.h" ModelFactoryMesh::Color Style::parseColour(const std::string_view & in) { if (in.empty()) { - return {}; + throw std::runtime_error("Empty colour specification"); } if (in[0] == '#') { + if (in.length() > 9 || in.length() % 2 == 0) { + throw std::runtime_error("Invalid hex colour specification"); + } ModelFactoryMesh::Color out {0, 0, 0, 1}; std::generate_n(out.begin(), (in.length() - 1) / 2, [in = in.data() + 1]() mutable { uint8_t channel; @@ -16,7 +20,13 @@ Style::parseColour(const std::string_view & in) }); return out; } - return {}; + if (auto mf = std::dynamic_pointer_cast<const AssetFactory>(Persistence::sharedObjects.at("assetFactory"))) { + if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) { + const auto out = glm::vec3 {colour->second} / 256.F; + return {out.r, out.g, out.b, 1.f}; + } + } + throw std::runtime_error("No such asset factory colour"); } void diff --git a/res/brush47.xml b/res/brush47.xml index 1ece78f..477610c 100644 --- a/res/brush47.xml +++ b/res/brush47.xml @@ -16,15 +16,15 @@ <mesh id="body" size="2.69,19.38,3.9"> <use type="cuboid" position="0,0,1.2" scale="2.69,19.38,1.5" colour="#2c4f5a"> <face id="bottom" colour="#2c3539"/> - <face id="front" colour="#fbc32f"/> - <face id="back" colour="#fbc32f"/> + <face id="front" colour="goldenrod"/> + <face id="back" colour="goldenrod"/> <face id="top" type="extrude" scale="1,0.95,1" position="0,0,0.8"> <face id="front" colour="#e1eff3"/> <face id="back" colour="#e1eff3"/> <face id="top" type="extrude" scale="0.5,0.8,0" position="0,0,0.4" smooth="true" colour="#aeb0b0"/> </face> </use> - <use type="cuboid" position="0,0,0.2" scale="2.6,4.5,1" colour="#413b3a"/> + <use type="cuboid" position="0,0,0.2" scale="2.6,4.5,1" colour="grey30"/> </mesh> <mesh id="bogie1"> <use type="bogie" position="0,6,0" colour="#413b3a"/> |