From fe76c06b30c9e42d58bfa8cc2efb2214ef55442a Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Thu, 30 May 2013 09:48:17 +0000 Subject: [PATCH] UI: support 3 digit hex colors like HTML, e.g. #123 becomes #112233. Patch #35359 by Forest Ka. --- source/blender/blenlib/intern/math_color.c | 21 +++++++++++++++------ 1 file 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)