diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2013-05-30 13:48:17 +0400 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2013-05-30 13:48:17 +0400 |
commit | fe76c06b30c9e42d58bfa8cc2efb2214ef55442a (patch) | |
tree | 79fe10f83b49b9621dac353d95d77727e1f89bfd /source/blender/blenlib/intern/math_color.c | |
parent | cf5e979fb4cef064f60d901c89c2a8a2f039d410 (diff) |
UI: support 3 digit hex colors like HTML, e.g. #123 becomes #112233.
Patch #35359 by Forest Ka.
Diffstat (limited to 'source/blender/blenlib/intern/math_color.c')
-rw-r--r-- | source/blender/blenlib/intern/math_color.c | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index cc892795a46..3f802c492c2 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -197,17 +197,26 @@ void hex_to_rgb(char *hexcol, float *r, float *g, float *b) if (hexcol[0] == '#') hexcol++; if (sscanf(hexcol, "%02x%02x%02x", &ri, &gi, &bi) == 3) { - *r = ri * (1.0f / 255.0f); - *g = gi * (1.0f / 255.0f); - *b = bi * (1.0f / 255.0f); - CLAMP(*r, 0.0f, 1.0f); - CLAMP(*g, 0.0f, 1.0f); - CLAMP(*b, 0.0f, 1.0f); + /* six digit hex colors */ + } + else if (sscanf(hexcol, "%01x%01x%01x", &ri, &gi, &bi) == 3) { + /* three digit hex colors (#123 becomes #112233) */ + ri += ri << 4; + gi += gi << 4; + bi += bi << 4; } else { /* avoid using un-initialized vars */ *r = *g = *b = 0.0f; + return; } + + *r = ri * (1.0f / 255.0f); + *g = gi * (1.0f / 255.0f); + *b = bi * (1.0f / 255.0f); + CLAMP(*r, 0.0f, 1.0f); + CLAMP(*g, 0.0f, 1.0f); + CLAMP(*b, 0.0f, 1.0f); } void rgb_to_hsv(float r, float g, float b, float *lh, float *ls, float *lv) |