summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-02-27 19:38:14 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2023-02-27 19:38:14 +0000
commitb7b9eb7bb9deb6704f276fb3b8fd67ba370caf3e (patch)
treecfbb3bac94271c04a44610f5f40751e74ea4d227
parentoperator*= can work on any iterable collection (diff)
downloadilt-b7b9eb7bb9deb6704f276fb3b8fd67ba370caf3e.tar.bz2
ilt-b7b9eb7bb9deb6704f276fb3b8fd67ba370caf3e.tar.xz
ilt-b7b9eb7bb9deb6704f276fb3b8fd67ba370caf3e.zip
Load the X11 RGB colour definitions into a map
-rw-r--r--assetFactory/assetFactory.cpp28
-rw-r--r--assetFactory/assetFactory.h6
-rw-r--r--test/fixtures/rgb.txt20
-rw-r--r--test/test-assetFactory.cpp30
4 files changed, 83 insertions, 1 deletions
diff --git a/assetFactory/assetFactory.cpp b/assetFactory/assetFactory.cpp
index 564ea28..016f29e 100644
--- a/assetFactory/assetFactory.cpp
+++ b/assetFactory/assetFactory.cpp
@@ -1,4 +1,5 @@
#include "assetFactory.h"
+#include "collections.hpp"
#include "cuboid.h"
#include "cylinder.h"
#include "modelFactoryMesh_fwd.h"
@@ -12,7 +13,8 @@ AssetFactory::AssetFactory() :
{"plane", std::make_shared<Plane>()},
{"cuboid", std::make_shared<Cuboid>()},
{"cylinder", std::make_shared<Cylinder>()},
- }
+ },
+ colours {parseX11RGB("/usr/share/X11/rgb.txt")}
{
}
@@ -23,6 +25,30 @@ AssetFactory::loadXML(const std::filesystem::path & filename)
return Persistence::SAXParsePersistence {}.loadState<std::shared_ptr<AssetFactory>>(file);
}
+AssetFactory::Colours
+AssetFactory::parseX11RGB(const char * path)
+{
+ filesystem::FileStar rgb {path, "r"};
+ Colours out;
+ glm::u8vec3 colour;
+ char inname[BUFSIZ];
+ while (fscanf(rgb, "%hhu %hhu %hhu %[^\n\r]s", &colour.r, &colour.g, &colour.b, inname) == 4) {
+ std::string name {inname};
+ normalizeColourName(name);
+ out.emplace(std::move(name), colour);
+ }
+ return out;
+}
+
+void
+AssetFactory::normalizeColourName(std::string & name)
+{
+ std::erase_if(name, ::isblank);
+ name *= [l = std::locale {}](auto & ch) {
+ ch = std::tolower(ch, l);
+ };
+}
+
bool
AssetFactory::persist(Persistence::PersistenceStore & store)
{
diff --git a/assetFactory/assetFactory.h b/assetFactory/assetFactory.h
index 9d79827..a68b460 100644
--- a/assetFactory/assetFactory.h
+++ b/assetFactory/assetFactory.h
@@ -22,12 +22,18 @@ class AssetFactory : public Persistence::Persistable {
public:
using Shapes = std::map<std::string, Shape::Ptr, std::less<>>;
using Assets = std::map<std::string, Asset::Ptr, std::less<>>;
+ using Colour = glm::u8vec3;
+ using Colours = std::map<std::string, Colour, std::less<>>;
AssetFactory();
[[nodiscard]] static std::shared_ptr<AssetFactory> loadXML(const std::filesystem::path &);
Shapes shapes;
Assets assets;
+ Colours colours;
+
+ static Colours parseX11RGB(const char * rgbtxtpath);
+ static void normalizeColourName(std::string &);
private:
friend Persistence::SelectionPtrBase<std::shared_ptr<AssetFactory>, true>;
diff --git a/test/fixtures/rgb.txt b/test/fixtures/rgb.txt
new file mode 100644
index 0000000..2fab7af
--- /dev/null
+++ b/test/fixtures/rgb.txt
@@ -0,0 +1,20 @@
+127 255 0 chartreuse1
+190 190 190 x11 gray
+169 169 169 DarkGrey
+ 0 255 255 cyan
+173 173 173 gray68
+202 225 255 LightSteelBlue1
+ 72 209 204 medium turquoise
+224 238 224 honeydew2
+238 197 145 burlywood2
+205 133 63 peru
+ 28 28 28 gray11
+ 83 134 139 CadetBlue4
+139 76 57 salmon4
+238 232 170 pale goldenrod
+112 128 144 slate grey
+255 255 0 yellow1
+159 121 238 MediumPurple2
+190 190 190 gray
+ 66 66 66 grey26
+0 0 139 DarkBlue
diff --git a/test/test-assetFactory.cpp b/test/test-assetFactory.cpp
index fd7a41d..89f6bf0 100644
--- a/test/test-assetFactory.cpp
+++ b/test/test-assetFactory.cpp
@@ -11,6 +11,7 @@
#include "gfx/gl/sceneRenderer.h"
#include "lib/collection.hpp"
#include "lib/location.hpp"
+#include "lib/stream_support.hpp"
#include "testMainWindow.h"
#include "ui/applicationBase.h"
@@ -180,3 +181,32 @@ BOOST_AUTO_TEST_CASE(brush47xml)
render(20);
}
BOOST_AUTO_TEST_SUITE_END();
+
+template<typename T> using InOut = std::tuple<T, T>;
+BOOST_DATA_TEST_CASE(normalizeColourName,
+ boost::unit_test::data::make<InOut<std::string>>({
+ {"", ""},
+ {"black", "black"},
+ {" black ", "black"},
+ {" b l a c k ", "black"},
+ {" B L A c k ", "black"},
+ {"BLAck ", "black"},
+ {"BLACK ", "black"},
+ {"BlAck ", "black"},
+ {"Bl Ack ", "black"},
+ }),
+ in_, exp)
+{
+ auto in {in_};
+ BOOST_CHECK_NO_THROW(AssetFactory::normalizeColourName(in));
+ BOOST_CHECK_EQUAL(in, exp);
+}
+
+BOOST_AUTO_TEST_CASE(parseX11RGB)
+{
+ const auto parsedColours = AssetFactory::parseX11RGB(FIXTURESDIR "rgb.txt");
+ BOOST_REQUIRE_EQUAL(parsedColours.size(), 20);
+ BOOST_CHECK_EQUAL(parsedColours.at("cyan"), AssetFactory::Colour(0, 255, 255));
+ BOOST_CHECK_EQUAL(parsedColours.at("slategrey"), AssetFactory::Colour(112, 128, 144));
+ BOOST_CHECK_EQUAL(parsedColours.at("lightsteelblue1"), AssetFactory::Colour(202, 225, 255));
+}