From a325624b4a75e29bc4db29ad6fe572ed8bca61ac Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Sun, 11 May 2014 03:34:34 -0500 Subject: [PATCH] WINGs: Option for decimal or hexadecimal RGB colors The RGB panel of the WINGs color panel lists the red, green, and blue values as base 10 numbers. However, hexadecimal numbers are very common when dealing with RGB colors. This patch adds two radio buttons at the bottom of the RGB panel to allow users to choose their preferred number system. For version 2 of the patch: Based on Cristophe's suggestions regarding the new decimal/hexadecimal RGB color feature introduced in commit 83d8ad6, this patch changes rgbState from an int to an enum, converts some if statements to switches, and changes from atoi to strtol for the base 10 string conversion. In addition, the "hexadecimal" text was too wide for the default size of its radio button for some fonts, so both radio buttons are now resized to fit the width of the panel. --- WINGs/wcolorpanel.c | 112 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 94 insertions(+), 18 deletions(-) diff --git a/WINGs/wcolorpanel.c b/WINGs/wcolorpanel.c index 29777027..63892c46 100644 --- a/WINGs/wcolorpanel.c +++ b/WINGs/wcolorpanel.c @@ -161,6 +161,8 @@ typedef struct W_ColorPanel { WMButton *grayPresetBtn[7]; /* RGB Panel */ + WMButton *rgbDecB; + WMButton *rgbHexB; WMFrame *rgbFrm; WMLabel *rgbMinL; WMLabel *rgbMaxL; @@ -170,6 +172,10 @@ typedef struct W_ColorPanel { WMTextField *rgbRedT; WMTextField *rgbGreenT; WMTextField *rgbBlueT; + enum { + RGBdec, + RGBhex + } rgbState; /* CMYK Panel */ WMFrame *cmykFrm; @@ -313,6 +319,7 @@ static void grayBrightnessTextFieldCallback(void *observerData, WMNotification * static void rgbSliderCallback(WMWidget * w, void *data); static void rgbTextFieldCallback(void *observerData, WMNotification * notification); +static void rgbDecToHex(WMWidget *w, void *data); static void cmykSliderCallback(WMWidget * w, void *data); static void cmykTextFieldCallback(void *observerData, WMNotification * notification); @@ -703,6 +710,23 @@ static WMColorPanel *makeColorPanel(WMScreen * scrPtr, const char *name) WMMoveWidget(panel->rgbBlueT, 146, 57); WMSetTextFieldAlignment(panel->rgbBlueT, WALeft); WMAddNotificationObserver(rgbTextFieldCallback, panel, WMTextDidEndEditingNotification, panel->rgbBlueT); + + panel->rgbDecB = WMCreateButton(panel->rgbFrm, WBTRadio); + WMSetButtonText(panel->rgbDecB, "Decimal"); + WMSetButtonSelected(panel->rgbDecB, 1); + panel->rgbState = RGBdec; + WMSetButtonAction(panel->rgbDecB, rgbDecToHex, panel); + WMResizeWidget(panel->rgbDecB, PWIDTH - 8, 23); + WMMoveWidget(panel->rgbDecB, 2, 81); + + panel->rgbHexB = WMCreateButton(panel->rgbFrm, WBTRadio); + WMSetButtonText(panel->rgbHexB, "Hexadecimal"); + WMSetButtonAction(panel->rgbHexB, rgbDecToHex, panel); + WMResizeWidget(panel->rgbHexB, PWIDTH - 8, 23); + WMMoveWidget(panel->rgbHexB, 2, 104); + + WMGroupButtons(panel->rgbDecB, panel->rgbHexB); + /* End of RGB Panel */ /* Widgets for CMYK Panel */ @@ -2348,11 +2372,53 @@ static void grayBrightnessTextFieldCallback(void *observerData, WMNotification * /******************* RGB Panel Functions *****************/ +void rgbIntToChar(W_ColorPanel *panel, int *value) +{ + char tmp[4]; + const char *format; + + switch (panel->rgbState) { + case RGBdec: + format = "%d"; + break; + case RGBhex: + format = "%0X"; + break; + } + + sprintf(tmp, format, value[0]); + WMSetTextFieldText(panel->rgbRedT, tmp); + sprintf(tmp, format, value[1]); + WMSetTextFieldText(panel->rgbGreenT, tmp); + sprintf(tmp, format, value[2]); + WMSetTextFieldText(panel->rgbBlueT, tmp); +} + +int *rgbCharToInt(W_ColorPanel *panel) +{ + int base = 0; + static int value[3]; + + switch (panel->rgbState) { + case RGBdec: + base = 10; + break; + case RGBhex: + base = 16; + break; + } + + value[0] = strtol(WMGetTextFieldText(panel->rgbRedT), NULL, base); + value[1] = strtol(WMGetTextFieldText(panel->rgbGreenT), NULL, base); + value[2] = strtol(WMGetTextFieldText(panel->rgbBlueT), NULL, base); + + return value; +} + static void rgbSliderCallback(WMWidget * w, void *data) { CPColor cpColor; int value[3]; - char tmp[4]; W_ColorPanel *panel = (W_ColorPanel *) data; /* Parameter not used, but tell the compiler that it is ok */ @@ -2362,12 +2428,7 @@ static void rgbSliderCallback(WMWidget * w, void *data) value[1] = WMGetSliderValue(panel->rgbGreenS); value[2] = WMGetSliderValue(panel->rgbBlueS); - sprintf(tmp, "%d", value[0]); - WMSetTextFieldText(panel->rgbRedT, tmp); - sprintf(tmp, "%d", value[1]); - WMSetTextFieldText(panel->rgbGreenT, tmp); - sprintf(tmp, "%d", value[2]); - WMSetTextFieldText(panel->rgbBlueT, tmp); + rgbIntToChar(panel, value); cpColor.rgb.red = value[0]; cpColor.rgb.green = value[1]; @@ -2381,17 +2442,14 @@ static void rgbSliderCallback(WMWidget * w, void *data) static void rgbTextFieldCallback(void *observerData, WMNotification * notification) { CPColor cpColor; - int value[3]; - char tmp[4]; + int *value; int n; W_ColorPanel *panel = (W_ColorPanel *) observerData; /* Parameter not used, but tell the compiler that it is ok */ (void) notification; - value[0] = atoi(WMGetTextFieldText(panel->rgbRedT)); - value[1] = atoi(WMGetTextFieldText(panel->rgbGreenT)); - value[2] = atoi(WMGetTextFieldText(panel->rgbBlueT)); + value = rgbCharToInt(panel); for (n = 0; n < 3; n++) { if (value[n] > 255) @@ -2400,12 +2458,7 @@ static void rgbTextFieldCallback(void *observerData, WMNotification * notificati value[n] = 0; } - sprintf(tmp, "%d", value[0]); - WMSetTextFieldText(panel->rgbRedT, tmp); - sprintf(tmp, "%d", value[1]); - WMSetTextFieldText(panel->rgbGreenT, tmp); - sprintf(tmp, "%d", value[2]); - WMSetTextFieldText(panel->rgbBlueT, tmp); + rgbIntToChar(panel, value); WMSetSliderValue(panel->rgbRedS, value[0]); WMSetSliderValue(panel->rgbGreenS, value[1]); @@ -2420,6 +2473,29 @@ static void rgbTextFieldCallback(void *observerData, WMNotification * notificati panel->lastChanged = WMRGBModeColorPanel; } +static void rgbDecToHex(WMWidget *w, void *data) +{ + W_ColorPanel *panel = (W_ColorPanel *) data; + int *value; + + (void) w; + + if (WMGetButtonSelected(panel->rgbDecB) && panel->rgbState == RGBhex) { + WMSetLabelText(panel->rgbMaxL, "255"); + WMRedisplayWidget(panel->rgbMaxL); + value = rgbCharToInt(panel); + panel->rgbState = RGBdec; + rgbIntToChar(panel, value); + } + if (WMGetButtonSelected(panel->rgbHexB) && panel->rgbState == RGBdec) { + WMSetLabelText(panel->rgbMaxL, "FF"); + WMRedisplayWidget(panel->rgbMaxL); + value = rgbCharToInt(panel); + panel->rgbState = RGBhex; + rgbIntToChar(panel, value); + } +} + /******************* CMYK Panel Functions *****************/ static void cmykSliderCallback(WMWidget * w, void *data)