From: Matthew Fernandez Date: Sun, 29 Jan 2023 01:26:44 +0000 (-0800) Subject: support an alpha channel when parsing HSV colors X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3513073a85a99420228cd481613891a169aa6f91;p=graphviz support an alpha channel when parsing HSV colors Mysteriously c4205c6e132efe64f23211fe885ff37209bc6ac0 implemented this, but only for GVPR programs. As a result, full HSVA colors could be used in GVPR programs but specifying an HSVA color in DOT required manually translating the color to RGBA. This change aligns DOT color parsing with how GVPR color parsing works. Gitlab: closes #510 Reported-by: Ryan Schmidt --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d2c0b1c0..a5db79d68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased (8.0.0)] +### Added + +- When specifying a color in HSV format, it is now possible to give an + additional fourth component for the alpha channel (opacity). #510 + ### Changed - The VML output renderer has been removed. This format has been superseded by diff --git a/lib/common/colxlate.c b/lib/common/colxlate.c index 85aa74f91..c4e3d5b28 100644 --- a/lib/common/colxlate.c +++ b/lib/common/colxlate.c @@ -326,24 +326,26 @@ int colorxlate(char *str, gvcolor_t * color, color_type_t target_type) } *q = '\0'; - if (sscanf(canon, "%lf%lf%lf", &H, &S, &V) == 3) { + A = 1.0; // default + if (sscanf(canon, "%lf%lf%lf%lf", &H, &S, &V, &A) >= 3) { /* clip to reasonable values */ H = fmax(fmin(H, 1.0), 0.0); S = fmax(fmin(S, 1.0), 0.0); V = fmax(fmin(V, 1.0), 0.0); + A = fmax(fmin(A, 1.0), 0.0); switch (target_type) { case HSVA_DOUBLE: color->u.HSVA[0] = H; color->u.HSVA[1] = S; color->u.HSVA[2] = V; - color->u.HSVA[3] = 1.0; /* opaque */ + color->u.HSVA[3] = A; break; case RGBA_BYTE: hsv2rgb(H, S, V, &R, &G, &B); color->u.rgba[0] = (int) (R * 255); color->u.rgba[1] = (int) (G * 255); color->u.rgba[2] = (int) (B * 255); - color->u.rgba[3] = 255; /* opaque */ + color->u.rgba[3] = (int) (A * 255); break; case CMYK_BYTE: hsv2rgb(H, S, V, &R, &G, &B); @@ -358,14 +360,14 @@ int colorxlate(char *str, gvcolor_t * color, color_type_t target_type) color->u.rrggbbaa[0] = (int) (R * 65535); color->u.rrggbbaa[1] = (int) (G * 65535); color->u.rrggbbaa[2] = (int) (B * 65535); - color->u.rrggbbaa[3] = 65535; /* opaque */ + color->u.rrggbbaa[3] = (int) (A * 65535); break; case RGBA_DOUBLE: hsv2rgb(H, S, V, &R, &G, &B); color->u.RGBA[0] = R; color->u.RGBA[1] = G; color->u.RGBA[2] = B; - color->u.RGBA[3] = 1.0; /* opaque */ + color->u.RGBA[3] = A; break; case COLOR_STRING: break; diff --git a/tests/test_regression.py b/tests/test_regression.py index a486f82d7..f47efb7a5 100644 --- a/tests/test_regression.py +++ b/tests/test_regression.py @@ -325,7 +325,6 @@ def test_358(): assert m is not None, \ f"font characteristic {1 << i} not enabled in xdot 1.7" -@pytest.mark.xfail(strict=True) def test_510(): """ HSV colors should also support an alpha channel