Skip to content

Commit d4afe5b

Browse files
committed
Random color
1 parent e2caeba commit d4afe5b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

wled00/json.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ static bool deserializeSegment(JsonObject elem, byte it, byte presetId)
214214
// JSON "col" array can contain the following values for each of segment's colors (primary, background, custom):
215215
// "col":[int|string|object|array, int|string|object|array, int|string|object|array]
216216
// int = Kelvin temperature or 0 for black
217-
// string = hex representation of [WW]RRGGBB
217+
// string = hex representation of [WW]RRGGBB or "r" for random color
218218
// object = individual channel control {"r":0,"g":127,"b":255,"w":255}, each being optional (valid to send {})
219219
// array = direct channel values [r,g,b,w] (w element being optional)
220-
int rgbw[] = {0,0,0,0};
220+
int rgbw[] = {0,0,0,seg.colors[i].a};
221221
bool colValid = false;
222222
JsonArray colX = colarr[i];
223223
if (colX.isNull()) {
@@ -227,19 +227,22 @@ static bool deserializeSegment(JsonObject elem, byte it, byte presetId)
227227
rgbw[0] = oCol["r"] | seg.colors[i].r;
228228
rgbw[1] = oCol["g"] | seg.colors[i].g;
229229
rgbw[2] = oCol["b"] | seg.colors[i].b;
230-
rgbw[3] = oCol["w"] | seg.colors[i].a;
230+
if (seg.hasWhite()) rgbw[3] = oCol["w"] | seg.colors[i].a;
231231
colValid = true;
232232
} else {
233-
byte brgbw[] = {0,0,0,0};
233+
byte brgbw[] = {0,0,0,seg.colors[i].a};
234234
const char* hexCol = colarr[i];
235235
if (hexCol == nullptr) { //Kelvin color temperature (or invalid), e.g 2400
236236
int kelvin = colarr[i] | -1;
237237
if (kelvin < 0) continue;
238-
if (kelvin == 0) seg.setColor(i, 0);
239238
if (kelvin > 0) colorKtoRGB(kelvin, brgbw);
240239
colValid = true;
241240
} else { //HEX string, e.g. "FFAA00"
242241
colValid = colorFromHexString(brgbw, hexCol);
242+
if (!colValid && tolower(hexCol[0]) == 'r') {
243+
setRandomColor(brgbw); // "random" color
244+
colValid = true;
245+
}
243246
}
244247
for (size_t c = 0; c < 4; c++) rgbw[c] = brgbw[c];
245248
}

0 commit comments

Comments
 (0)