FastLED 3.10.6
Loading...
Searching...
No Matches
crgb_json.cpp.hpp
Go to the documentation of this file.
1#include "fl/gfx/crgb_json.h"
2
3namespace fl {
4
5namespace {
6
8 if (!value.is_int() || value.is_bool()) {
9 return fl::nullopt;
10 }
11 const optional<i64> channel = value.as<i64>();
12 if (!channel || *channel < 0 || *channel > 255) {
13 return fl::nullopt;
14 }
15 return channel;
16}
17
19 if (!value.is_array() || value.size() != 3) {
20 return fl::nullopt;
21 }
22
23 const optional<i64> red = crgbJsonChannel(value[0]);
24 const optional<i64> green = crgbJsonChannel(value[1]);
25 const optional<i64> blue = crgbJsonChannel(value[2]);
26 if (!red || !green || !blue) {
27 return fl::nullopt;
28 }
29
30 return CRGB(static_cast<u8>(*red), static_cast<u8>(*green),
31 static_cast<u8>(*blue));
32}
33
34bool crgbJsonMatches(const json& document,
36 if (!document.is_object() || document.size() != 2) {
37 return false;
38 }
39
40 const json versionJson = document["version"];
41 const optional<i64> version = versionJson.as<i64>();
42 if (!versionJson.is_int() || versionJson.is_bool() || !version ||
43 *version != 1) {
44 return false;
45 }
46
47 const json encodedPixels = document["pixels"];
48 if (!encodedPixels.is_array() || encodedPixels.size() != pixels.size()) {
49 return false;
50 }
51
52 for (size i = 0; i < encodedPixels.size(); ++i) {
53 const optional<CRGB> decoded = crgbJsonPixel(encodedPixels[i]);
54 if (!decoded || *decoded != pixels[i]) {
55 return false;
56 }
57 }
58 return true;
59}
60
61} // namespace
62
64 json encodedPixels = json::array();
65 if (!encodedPixels.is_array()) {
66 return fl::nullopt;
67 }
68
69 for (const CRGB& pixel : pixels) {
70 json encodedPixel = json::array();
71 if (!encodedPixel.is_array()) {
72 return fl::nullopt;
73 }
74
75 encodedPixel.push_back(json(static_cast<int>(pixel.r)));
76 encodedPixel.push_back(json(static_cast<int>(pixel.g)));
77 encodedPixel.push_back(json(static_cast<int>(pixel.b)));
78 if (encodedPixel.size() != 3 || !crgbJsonPixel(encodedPixel)) {
79 return fl::nullopt;
80 }
81
82 const size oldSize = encodedPixels.size();
83 encodedPixels.push_back(encodedPixel);
84 if (encodedPixels.size() != oldSize + 1) {
85 return fl::nullopt;
86 }
87 }
88
89 json document = json::object();
90 if (!document.is_object()) {
91 return fl::nullopt;
92 }
93 document.set("version", 1);
94 document.set("pixels", encodedPixels);
95 if (!crgbJsonMatches(document, pixels)) {
96 return fl::nullopt;
97 }
98 return document;
99}
100
101bool crgbFromJson(const json& document, span<CRGB> pixels) FL_NO_EXCEPT {
102 if (!document.is_object()) {
103 return false;
104 }
105
106 const json versionJson = document["version"];
107 const optional<i64> version = versionJson.as<i64>();
108 if (!versionJson.is_int() || versionJson.is_bool() || !version ||
109 *version != 1) {
110 return false;
111 }
112
113 const json encodedPixels = document["pixels"];
114 if (!encodedPixels.is_array() || encodedPixels.size() != pixels.size()) {
115 return false;
116 }
117
118 for (size i = 0; i < encodedPixels.size(); ++i) {
119 if (!crgbJsonPixel(encodedPixels[i])) {
120 return false;
121 }
122 }
123
124 for (size i = 0; i < encodedPixels.size(); ++i) {
125 pixels[i] = *crgbJsonPixel(encodedPixels[i]);
126 }
127 return true;
128}
129
130} // namespace fl
bool is_object() const FL_NO_EXCEPT
Definition json.h:391
size_t size() const FL_NO_EXCEPT
Definition json.h:798
void set(const fl::string &key, const json &value) FL_NO_EXCEPT
Definition json.h:866
bool is_int() const FL_NO_EXCEPT
Definition json.h:383
bool is_array() const FL_NO_EXCEPT
Definition json.h:389
void push_back(const json &value) FL_NO_EXCEPT
Definition json.h:918
bool is_bool() const FL_NO_EXCEPT
Definition json.h:382
fl::optional< T > as() const FL_NO_EXCEPT
Definition json.h:490
static json object() FL_NO_EXCEPT
Definition json.h:857
static json array() FL_NO_EXCEPT
Definition json.h:853
JSON serialization helpers for CRGB pixel buffers.
optional< i64 > crgbJsonChannel(const json &value) FL_NO_EXCEPT
optional< CRGB > crgbJsonPixel(const json &value) FL_NO_EXCEPT
bool crgbJsonMatches(const json &document, span< const CRGB > pixels) FL_NO_EXCEPT
unsigned char u8
Definition stdint.h:131
fl::CRGB CRGB
Definition video.h:18
constexpr int type_rank< T >::value
optional< json > crgbToJson(span< const CRGB > pixels) FL_NO_EXCEPT
Encode a CRGB pixel buffer as a versioned JSON object.
Optional< T > optional
Definition optional.h:16
InputGamut g FL_NO_EXCEPT
Definition rgbw.h:121
constexpr nullopt_t nullopt
Definition optional.h:13
bool crgbFromJson(const json &document, span< CRGB > pixels) FL_NO_EXCEPT
Decode a versioned JSON object into an existing CRGB pixel buffer.
Base definition for an LED controller.
Definition crgb.hpp:179
fl::i64 i64
Definition stdint.h:221
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38