00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023 #include "scbcolor.h"
00024 #include "scblog.h"
00025 #include <stdio.h>
00026 #include "scbtype.h"
00027 #include "scbconfig.h"
00028
00029 void scb_color_set_white(ScbColorPtr ptr)
00030 {
00031 if (NULL == ptr) return;
00032 ptr->red = 0xff;
00033 ptr->green = 0xff;
00034 ptr->blue = 0xff;
00035 ptr->pixel = 0xffff;
00036 ptr->pad = 0;
00037 ptr->flags = 0;
00038 }
00039
00040 void scb_color_set_black(ScbColorPtr ptr)
00041 {
00042 if (NULL == ptr) return;
00043 ptr->red = 0;
00044 ptr->green = 0;
00045 ptr->blue = 0;
00046 ptr->pixel = 0;
00047 ptr->pad = 0;
00048 ptr->flags = 0;
00049 }
00050
00051 void scb_color_set_light_gray(ScbColorPtr ptr)
00052 {
00053 if (NULL == ptr) return;
00054 ptr->red = 0xaa;
00055 ptr->green = 0xaa;
00056 ptr->blue = 0xaa;
00057 ptr->pixel = 0xaaaa;
00058 ptr->pad = 0;
00059 ptr->flags = 0;
00060 }
00061
00062 void scb_color_set_dark_gray(ScbColorPtr ptr)
00063 {
00064 if (NULL == ptr) return;
00065 ptr->red = 0x55;
00066 ptr->green = 0x55;
00067 ptr->blue = 0x55;
00068 ptr->pixel = 0x5555;
00069 ptr->pad = 0;
00070 ptr->flags = 0;
00071 }
00072
00073 char _g_htmlColor[SCB_MAX_COLOR_LEN] = {0};
00074
00075
00076 const char * scb_color_to_html_color(const ScbColorPtr ptr)
00077 {
00078 SCB_RET_NULL_IF(NULL == ptr, "Invalid color pointer!");
00079 snprintf(_g_htmlColor, SCB_MAX_COLOR_LEN,
00080 "#%02X%02X%02X", ptr->red, ptr->green, ptr->blue);
00081 return _g_htmlColor;
00082 }
00083
00084
00085 ScbDevColor scb_color_to_dev_color(ScbColorPtr ptr)
00086 {
00087 if (NULL == ptr) return SCB_DEV_COLOR_UNKNOWN;
00088 if (0 == ptr->red && 0 == ptr->green && 0 == ptr->blue)
00089 {
00090 return SCB_DEV_COLOR_BLACK;
00091 }
00092 else if (0xff == ptr->red && 0xff == ptr->green && 0xff == ptr->blue)
00093 {
00094 return SCB_DEV_COLOR_WHITE;
00095 }
00096 else if (0xaa == ptr->red && 0xaa == ptr->green && 0xaa == ptr->blue)
00097 {
00098 return SCB_DEV_COLOR_LIGHT_GRAY;
00099 }
00100 else if (0x55 == ptr->red && 0x55 == ptr->green && 0x55 == ptr->blue)
00101 {
00102 return SCB_DEV_COLOR_DARK_GRAY;
00103 }
00104 return SCB_DEV_COLOR_UNKNOWN;
00105 }
00106
00107
00108
00109 int scb_html_color_to_color(const char * buf, const int length, ScbColorPtr ptr)
00110 {
00111 if (NULL == ptr)
00112 {
00113 SCB_ERROR("Invalid color pointer!");
00114 return SCB_RET_ERR;
00115 }
00116
00117 if (NULL == buf || 0 >= length)
00118 {
00119 SCB_ERROR("Buffer or lenght is invalid!");
00120 return SCB_RET_ERR;
00121 }
00122 int r = 0, g = 0, b = 0;
00123 sscanf(buf, "#%02x%02x%02x", &r, &g, &b);
00124 ptr->red = (unsigned short)r;
00125 ptr->green = (unsigned short)g;
00126 ptr->blue = (unsigned short)b;
00127 return SCB_RET_OK;
00128 }
00129
00130 ScbDevColor scb_html_color_to_dev_color(const char *buf, const int length)
00131 {
00132 if (NULL == buf || 0 >= length)
00133 {
00134 SCB_ERROR("Buffer or lenght is invalid!");
00135 return SCB_DEV_COLOR_UNKNOWN;
00136 }
00137 int r = 0, g = 0, b = 0;
00138 ScbColor color;
00139 sscanf(buf, "#%02x%02x%02x", &r, &g, &b);
00140 color.red = (unsigned short)r;
00141 color.green = (unsigned short)g;
00142 color.blue = (unsigned short)b;
00143 return scb_color_to_dev_color(&color);
00144 }
00145
00146
00147
00148 void scb_dev_color_to_color(ScbColorPtr ptr, const ScbDevColor color)
00149 {
00150 if (NULL == ptr) return;
00151 switch(color)
00152 {
00153 case SCB_DEV_COLOR_WHITE :
00154 scb_color_set_white(ptr);
00155 break;
00156 case SCB_DEV_COLOR_LIGHT_GRAY :
00157 scb_color_set_light_gray(ptr);
00158 break;
00159 case SCB_DEV_COLOR_DARK_GRAY :
00160 scb_color_set_dark_gray(ptr);
00161 break;
00162 default:
00163 scb_color_set_black(ptr);
00164 break;
00165 }
00166 }
00167
00168 const char * scb_dev_color_to_html_color(const ScbDevColor color)
00169 {
00170 if (SCB_DEV_COLOR_WHITE == color)
00171 {
00172 snprintf(_g_htmlColor, SCB_MAX_COLOR_LEN,
00173 "#%02X%02X%02X", 0xff, 0xff, 0xff);
00174 }
00175 else if (SCB_DEV_COLOR_LIGHT_GRAY == color)
00176 {
00177 snprintf(_g_htmlColor, SCB_MAX_COLOR_LEN,
00178 "#%02X%02X%02X", 0xaa, 0xaa, 0xaa);
00179 }
00180 else if (SCB_DEV_COLOR_DARK_GRAY == color)
00181 {
00182 snprintf(_g_htmlColor, SCB_MAX_COLOR_LEN,
00183 "#%02X%02X%02X", 0x55, 0x55, 0x55);
00184 }
00185 else
00186 {
00187 snprintf(_g_htmlColor, SCB_MAX_COLOR_LEN,
00188 "#%02X%02X%02X", 0x00, 0x00, 0x00);
00189 }
00190 return _g_htmlColor;
00191 }
00192
00193
00194