Skip to content

Commit

Permalink
Refactor changes in Mapscript colorObj to optional arguments
Browse files Browse the repository at this point in the history
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 Jul 20, 2015
1 parent 025ffe1 commit d7c26bc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 106 deletions.
86 changes: 20 additions & 66 deletions mapscript/php/color.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ ZEND_BEGIN_ARG_INFO_EX(color_setRGB_args, 0, 0, 3)
ZEND_ARG_INFO(0, red) ZEND_ARG_INFO(0, red)
ZEND_ARG_INFO(0, green) ZEND_ARG_INFO(0, green)
ZEND_ARG_INFO(0, blue) 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_ARG_INFO(0, alpha)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()


Expand All @@ -62,9 +56,6 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(color_toHex_args, 0, 0, 0) ZEND_BEGIN_ARG_INFO_EX(color_toHex_args, 0, 0, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()


ZEND_BEGIN_ARG_INFO_EX(color_toHexWithAlpha_args, 0, 0, 0)
ZEND_END_ARG_INFO()

/* {{{ proto void __construct() /* {{{ proto void __construct()
colorObj CANNOT be instanciated, this will throw an exception on use */ colorObj CANNOT be instanciated, this will throw an exception on use */
PHP_METHOD(colorObj, __construct) PHP_METHOD(colorObj, __construct)
Expand Down Expand Up @@ -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. */ Set new RGB color. */
PHP_METHOD(colorObj, setRGB) PHP_METHOD(colorObj, setRGB)
{ {
zval *zobj = getThis(); zval *zobj = getThis();
long red, green, blue; 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",
&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;
php_color_object *php_color; php_color_object *php_color;


PHP_MAPSCRIPT_ERROR_HANDLING(TRUE); 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) { &red, &green, &blue, &alpha) == FAILURE) {
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE); PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
return; return;
Expand Down Expand Up @@ -220,10 +186,10 @@ PHP_METHOD(colorObj, setHex)
/* }}} */ /* }}} */


/* {{{ proto string color.toHex() /* {{{ proto string color.toHex()
Get hex string #rrggbb. */ Get hex string #rrggbb[aa]. */
PHP_METHOD(colorObj, toHex) PHP_METHOD(colorObj, toHex)
{ {
char hex[8] = ""; char *hex;
zval *zobj = getThis(); zval *zobj = getThis();
php_color_object *php_color; php_color_object *php_color;
colorObj *color; colorObj *color;
Expand All @@ -236,42 +202,30 @@ PHP_METHOD(colorObj, toHex)
return; return;
} }


snprintf(hex, 8, "#%02x%02x%02x", if (color->alpha == 255) {
color->red, color->green, color->blue); hex = msSmallMalloc(8);

snprintf(hex, 8, "#%02x%02x%02x",
RETURN_STRING(hex, 1); color->red, color->green, color->blue);
} } else if (color->alpha >= 0) {
/* }}} */ hex = msSmallMalloc(10);

snprintf(hex, 10, "#%02x%02x%02x%02x",
/* {{{ proto string color.toHexWithAlpha() color->red, color->green, color->blue, color->alpha);
Get hex string with alpha component #rrggbbaa. */ } else {
PHP_METHOD(colorObj, toHexWithAlpha) mapscript_throw_exception("Can't express color with invalid alpha as hex." TSRMLS_CC);
{
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);
return; return;
} }


snprintf(hex, 10, "#%02x%02x%02x%02x", RETURN_STRINGL(hex, strlen(hex), 0);
color->red, color->green, color->blue, color->alpha);

RETURN_STRING(hex, 1);
} }
/* }}} */ /* }}} */


zend_function_entry color_functions[] = { zend_function_entry color_functions[] = {
PHP_ME(colorObj, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(colorObj, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(colorObj, __get, color___get_args, ZEND_ACC_PUBLIC) PHP_ME(colorObj, __get, color___get_args, ZEND_ACC_PUBLIC)
PHP_ME(colorObj, __set, color___set_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 NULL, NULL, NULL
} }
}; };
Expand Down
58 changes: 18 additions & 40 deletions mapscript/swiginc/color.i
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,30 +61,18 @@
free(self); free(self);
} }


int setRGB(int red, int green, int blue) int setRGB(int red, int green, int blue, int alpha = 255)
{
/* 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)
{ {
/* Check colors */ /* Check colors */
if (red > 255 || green > 255 || blue > 255 || alpha > 255) { 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; return MS_FAILURE;
} }


MS_INIT_COLOR(*self, red, green, blue, alpha); MS_INIT_COLOR(*self, red, green, blue, alpha);
return MS_SUCCESS; return MS_SUCCESS;
} }

int setHex(char *psHexColor) int setHex(char *psHexColor)
{ {
int red, green, blue, alpha = 255; int red, green, blue, alpha = 255;
Expand Down Expand Up @@ -112,7 +100,7 @@
%newobject toHex; %newobject toHex;
char *toHex() char *toHex()
{ {
char hexcolor[8] = ""; char *hexcolor;


if (!self) if (!self)
{ {
Expand All @@ -126,31 +114,21 @@
"toHex()"); "toHex()");
return NULL; return NULL;
} }
snprintf(hexcolor, 8, "#%02x%02x%02x", if (self->alpha == 255) {
self->red, self->green, self->blue); hexcolor = msSmallMalloc(8);
return strdup(hexcolor); snprintf(hexcolor, 8, "#%02x%02x%02x",
} self->red, self->green, self->blue);

} else if (self->alpha >= 0) {
%newobject toHexWithAlpha; hexcolor = msSmallMalloc(10);
char *toHexWithAlpha() snprintf(hexcolor, 10, "#%02x%02x%02x%02x",
{ self->red, self->green, self->blue, self->alpha);
char hexcolor[10] = ""; } else {

msSetError(MS_MISCERR, "Can't express color with invalid alpha as hex",
if (!self) "toHex()");
{ return NULL;
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;
} }
snprintf(hexcolor, 10, "#%02x%02x%02x%02x", return hexcolor;
self->red, self->green, self->blue, self->alpha);
return strdup(hexcolor);
} }

} }


0 comments on commit d7c26bc

Please sign in to comment.