libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
xyzcolor.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <promeki/namespace.h>
14#include <promeki/array.h>
15#include <promeki/string.h>
16#include <promeki/error.h>
17#include <promeki/result.h>
18#include <promeki/datatype.h>
19#include <promeki/stringlist.h>
20
21PROMEKI_NAMESPACE_BEGIN
22
23class DataStream;
24
63class XYZColor {
64 public:
65 PROMEKI_DATATYPE(XYZColor, DataTypeXYZColor, 1)
66
67
68 Error writeToStream(DataStream &s) const;
70 template <uint32_t V> static Result<XYZColor> readFromStream(DataStream &s);
71
73 using DataType = Array<double, 3>;
74
76 XYZColor() : d{-1.0, -1.0, -1.0} {}
77
82 XYZColor(const DataType &val) : d(val) {}
83
90 XYZColor(double x, double y, double z) : d(x, y, z) {}
91
98 bool isValid() const {
99 for (size_t i = 0; i < d.size(); ++i)
100 if (d[i] < 0.0) return false;
101 return true;
102 }
103
105 const DataType &data() const { return d; }
107 DataType &data() { return d; }
109 double x() { return d[0]; }
111 double y() { return d[1]; }
113 double z() { return d[2]; }
115 void set(double x, double y, double z) { d = {x, y, z}; }
117 void setX(double val) { d[0] = val; }
119 void setY(double val) { d[1] = val; }
121 void setZ(double val) { d[2] = val; }
122
129 XYZColor lerp(const XYZColor &val, double t) const { return d.lerp(val.d, t); }
130
132 operator String() { return toString(); }
133
135 String toString() const { return String::sprintf("XYZ(%g, %g, %g)", d[0], d[1], d[2]); }
136
144 static Result<XYZColor> fromString(const String &s) {
145 String body = s.trim();
146 if (body.startsWith("XYZ(") || body.startsWith("xyz(")) body = body.mid(4);
147 if (body.startsWith("(")) body = body.mid(1);
148 if (body.endsWith(")")) body = body.left(body.length() - 1);
149 StringList parts = body.split(",");
150 if (parts.size() != 3) return makeError<XYZColor>(Error::ParseFailed);
151 XYZColor out;
152 for (size_t i = 0; i < 3; ++i) {
153 Error ex;
154 double v = parts[i].trim().to<double>(&ex);
155 if (ex.isError()) return makeError<XYZColor>(Error::ParseFailed);
156 out.d[i] = v;
157 }
158 return makeResult(std::move(out));
159 }
160
161 private:
162 DataType d;
163};
164
165PROMEKI_NAMESPACE_END
166
167PROMEKI_FORMAT_VIA_TOSTRING(promeki::XYZColor);
168
169#endif // PROMEKI_ENABLE_CORE