Permalink
Browse files

Refactor changes in Mapscript colorObj to optional arguments

Instead of introducing separate setRGBA and toHexWithAlpha we alter the
existing method setRGB to take an optional fourth argument and toHex such
that it outputs a 4-byte hex string if the alpha value is other than 255
and a 3-byte hex string as previously if the alpha is the default 255.
  • Loading branch information...
ejn committed Mar 6, 2015
1 parent 025ffe1 commit d7c26bcd686bf251a046e3ef4f61b8f623eca663
Showing with 38 additions and 106 deletions.
  1. +20 −66 mapscript/php/color.c
  2. +18 −40 mapscript/swiginc/color.i
View
@@ -46,12 +46,6 @@ ZEND_BEGIN_ARG_INFO_EX(color_setRGB_args, 0, 0, 3)
ZEND_ARG_INFO(0, red)
ZEND_ARG_INFO(0, green)
ZEND_ARG_INFO(0, blue)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(color_setRGBA_args, 0, 0, 4)
ZEND_ARG_INFO(0, red)
ZEND_ARG_INFO(0, green)
ZEND_ARG_INFO(0, blue)
ZEND_ARG_INFO(0, alpha)
ZEND_END_ARG_INFO()
@@ -62,9 +56,6 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(color_toHex_args, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(color_toHexWithAlpha_args, 0, 0, 0)
ZEND_END_ARG_INFO()
/* {{{ proto void __construct()
colorObj CANNOT be instanciated, this will throw an exception on use */
PHP_METHOD(colorObj, __construct)
@@ -127,41 +118,16 @@ PHP_METHOD(colorObj, __set)
}
/* {{{ proto int color.setRGB(int R, int G, int B)
/* {{{ proto int color.setRGB(int R, int G, int B, int A = 255)
Set new RGB color. */
PHP_METHOD(colorObj, setRGB)
{
zval *zobj = getThis();
long red, green, blue;
php_color_object *php_color;
PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll",
&red, &green, &blue) == FAILURE) {
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
return;
}
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
php_color = (php_color_object *) zend_object_store_get_object(zobj TSRMLS_CC);
MS_INIT_COLOR(*(php_color->color), red, green, blue,255);
RETURN_LONG(MS_SUCCESS);
}
/* }}} */
/* {{{ proto int color.setRGB(int R, int G, int B, int A)
Set new RGBA color. */
PHP_METHOD(colorObj, setRGBA)
{
zval *zobj = getThis();
long red, green, blue, alpha;
long red, green, blue, alpha = 255;
php_color_object *php_color;
PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll",
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll|l",
&red, &green, &blue, &alpha) == FAILURE) {
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
return;
@@ -220,10 +186,10 @@ PHP_METHOD(colorObj, setHex)
/* }}} */
/* {{{ proto string color.toHex()
Get hex string #rrggbb. */
Get hex string #rrggbb[aa]. */
PHP_METHOD(colorObj, toHex)
{
char hex[8] = "";
char *hex;
zval *zobj = getThis();
php_color_object *php_color;
colorObj *color;
@@ -236,42 +202,30 @@ PHP_METHOD(colorObj, toHex)
return;
}
snprintf(hex, 8, "#%02x%02x%02x",
color->red, color->green, color->blue);
RETURN_STRING(hex, 1);
}
/* }}} */
/* {{{ proto string color.toHexWithAlpha()
Get hex string with alpha component #rrggbbaa. */
PHP_METHOD(colorObj, toHexWithAlpha)
{
char hex[8] = "";
zval *zobj = getThis();
php_color_object *php_color;
colorObj *color;
php_color = (php_color_object *) zend_object_store_get_object(zobj TSRMLS_CC);
color = php_color->color;
if (color->red < 0 || color->green < 0 || color->blue < 0 || color->alpha < 0) {
mapscript_throw_exception("Can't express invalid color as hex." TSRMLS_CC);
if (color->alpha == 255) {
hex = msSmallMalloc(8);
snprintf(hex, 8, "#%02x%02x%02x",
color->red, color->green, color->blue);
} else if (color->alpha >= 0) {
hex = msSmallMalloc(10);
snprintf(hex, 10, "#%02x%02x%02x%02x",
color->red, color->green, color->blue, color->alpha);
} else {
mapscript_throw_exception("Can't express color with invalid alpha as hex." TSRMLS_CC);
return;
}
snprintf(hex, 10, "#%02x%02x%02x%02x",
color->red, color->green, color->blue, color->alpha);
RETURN_STRING(hex, 1);
RETURN_STRINGL(hex, strlen(hex), 0);
}
/* }}} */
zend_function_entry color_functions[] = {
PHP_ME(colorObj, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(colorObj, __get, color___get_args, ZEND_ACC_PUBLIC)
PHP_ME(colorObj, __set, color___set_args, ZEND_ACC_PUBLIC)
PHP_ME(colorObj, setRGB, color_setRGB_args, ZEND_ACC_PUBLIC) {
PHP_ME(colorObj, setRGB, color_setRGB_args, ZEND_ACC_PUBLIC)
PHP_ME(colorObj, setHex, color_setHex_args, ZEND_ACC_PUBLIC)
PHP_ME(colorObj, toHex, color_toHex_args, ZEND_ACC_PUBLIC) {
NULL, NULL, NULL
}
};
View
@@ -61,30 +61,18 @@
free(self);
}
int setRGB(int red, int green, int blue)
{
/* Check colors */
if (red > 255 || green > 255 || blue > 255) {
msSetError(MS_MISCERR, "Invalid color index.", "setRGB()");
return MS_FAILURE;
}
MS_INIT_COLOR(*self, red, green, blue, 255);
return MS_SUCCESS;
}
int setRGBA(int red, int green, int blue, int alpha)
int setRGB(int red, int green, int blue, int alpha = 255)
{
/* Check colors */
if (red > 255 || green > 255 || blue > 255 || alpha > 255) {
msSetError(MS_MISCERR, "Invalid color index.", "setRGBA()");
msSetError(MS_MISCERR, "Invalid color index.", "setRGB()");
return MS_FAILURE;
}
MS_INIT_COLOR(*self, red, green, blue, alpha);
return MS_SUCCESS;
}
int setHex(char *psHexColor)
{
int red, green, blue, alpha = 255;
@@ -112,7 +100,7 @@
%newobject toHex;
char *toHex()
{
char hexcolor[8] = "";
char *hexcolor;
if (!self)
{
@@ -126,31 +114,21 @@
"toHex()");
return NULL;
}
snprintf(hexcolor, 8, "#%02x%02x%02x",
self->red, self->green, self->blue);
return strdup(hexcolor);
}
%newobject toHexWithAlpha;
char *toHexWithAlpha()
{
char hexcolor[10] = "";
if (!self)
{
msSetError(MS_MISCERR, "Can't express NULL color as hex",
"toHexWithAlpha()");
return NULL;
}
if (self->red < 0 || self->green < 0 || self->blue < 0 || self->alpha < 0)
{
msSetError(MS_MISCERR, "Can't express invalid color as hex",
"toHexWithAlpha()");
return NULL;
if (self->alpha == 255) {
hexcolor = msSmallMalloc(8);
snprintf(hexcolor, 8, "#%02x%02x%02x",
self->red, self->green, self->blue);
} else if (self->alpha >= 0) {
hexcolor = msSmallMalloc(10);
snprintf(hexcolor, 10, "#%02x%02x%02x%02x",
self->red, self->green, self->blue, self->alpha);
} else {
msSetError(MS_MISCERR, "Can't express color with invalid alpha as hex",
"toHex()");
return NULL;
}
snprintf(hexcolor, 10, "#%02x%02x%02x%02x",
self->red, self->green, self->blue, self->alpha);
return strdup(hexcolor);
return hexcolor;
}
}

0 comments on commit d7c26bc

Please sign in to comment.