Skip to content

Commit fee6d83

Browse files
committed
parse: properties: utils: Split out conversion of HSL to int
Having the fixed point representation of RGB available allows it to be reused for HWB.
1 parent 8501f0c commit fee6d83

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

src/parse/properties/utils.c

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -263,27 +263,28 @@ css_error css__parse_border_side(css_language *c,
263263
return error;
264264
}
265265

266-
267266
/**
268267
* Convert Hue Saturation Lightness value to RGB.
269268
*
270-
* \param hue Hue in degrees 0..360
271-
* \param sat Saturation value in percent 0..100
272-
* \param lit Lightness value in percent 0..100
273-
* \param r red component
274-
* \param g green component
275-
* \param b blue component
269+
* \param[in] hue Hue in degrees 0..360
270+
* \param[in] sat Saturation value in percent 0..100
271+
* \param[in] lit Lightness value in percent 0..100
272+
* \param[out] r red component (0..25500)
273+
* \param[out] g green component (0..25500)
274+
* \param[out] b blue component (0..25500)
276275
*/
277-
static void HSL_to_RGB(css_fixed hue, css_fixed sat, css_fixed lit, uint8_t *r, uint8_t *g, uint8_t *b)
276+
static void HSL_to_RGB_fixed(
277+
css_fixed hue, css_fixed sat, css_fixed lit,
278+
css_fixed *r, css_fixed *g, css_fixed *b)
278279
{
279280
css_fixed min_rgb, max_rgb, chroma;
280281
css_fixed relative_hue, scaled_hue, mid1, mid2;
281282
int sextant;
282283

283284
#define ORGB(R, G, B) \
284-
*r = FIXTOINT(FDIV(FMUL((R), F_255), F_100)); \
285-
*g = FIXTOINT(FDIV(FMUL((G), F_255), F_100)); \
286-
*b = FIXTOINT(FDIV(FMUL((B), F_255), F_100))
285+
*r = FMUL((R), F_255); \
286+
*g = FMUL((G), F_255); \
287+
*b = FMUL((B), F_255)
287288

288289
/* If saturation is zero there is no hue and r = g = b = lit */
289290
if (sat == INTTOFIX(0)) {
@@ -332,25 +333,48 @@ static void HSL_to_RGB(css_fixed hue, css_fixed sat, css_fixed lit, uint8_t *r,
332333
relative_hue = FSUB(hue, INTTOFIX(sextant));
333334

334335
/* Scale offset by chroma */
335-
scaled_hue = FMUL(relative_hue, chroma);
336+
scaled_hue = FMUL(relative_hue, chroma);
336337

337338
/* Compute potential values of the third colour component */
338-
mid1 = FADD(min_rgb, scaled_hue);
339-
mid2 = FSUB(max_rgb, scaled_hue);
339+
mid1 = FADD(min_rgb, scaled_hue);
340+
mid2 = FSUB(max_rgb, scaled_hue);
340341

341342
/* Populate result */
342-
switch (sextant) {
343+
switch (sextant) {
343344
case 0: ORGB(max_rgb, mid1, min_rgb); break;
344345
case 1: ORGB(mid2, max_rgb, min_rgb); break;
345346
case 2: ORGB(min_rgb, max_rgb, mid1); break;
346347
case 3: ORGB(min_rgb, mid2, max_rgb); break;
347348
case 4: ORGB(mid1, min_rgb, max_rgb); break;
348349
case 5: ORGB(max_rgb, min_rgb, mid2); break;
349-
}
350+
}
350351

351352
#undef ORGB
352353
}
353354

355+
/**
356+
* Convert Hue Saturation Lightness value to RGB.
357+
*
358+
* \param hue Hue in degrees 0..360
359+
* \param sat Saturation value in percent 0..100
360+
* \param lit Lightness value in percent 0..100
361+
* \param r red component
362+
* \param g green component
363+
* \param b blue component
364+
*/
365+
static void HSL_to_RGB(
366+
css_fixed hue, css_fixed sat, css_fixed lit,
367+
uint8_t *r, uint8_t *g, uint8_t *b)
368+
{
369+
css_fixed rf, gf, bf;
370+
371+
HSL_to_RGB_fixed(hue, sat, lit, &rf, &gf, &bf);
372+
373+
*r = FIXTOINT(FDIV(rf, F_100));
374+
*g = FIXTOINT(FDIV(gf, F_100));
375+
*b = FIXTOINT(FDIV(bf, F_100));
376+
}
377+
354378
/**
355379
* Parse a RGB(A) colour specifier
356380
*

0 commit comments

Comments
 (0)