Skip to content

Commit

Permalink
MapScript colorObj SWIG/PHP alignment and methods with alpha
Browse files Browse the repository at this point in the history
Changes in the SWIG bindings:
- add the methods setRGBA() and toHexWithAlpha()
- extend the method setHex() to accept #rrggbbaa hex values

Changes in the PHP bindings:
- add the methods setRGBA() and toHexWithAlpha()
- adds the methods toHex() and setHex() which were already in the SWIG bindings
  • Loading branch information
ejn committed Jul 20, 2015
1 parent a504deb commit 025ffe1
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 4 deletions.
132 changes: 132 additions & 0 deletions mapscript/php/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ 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()

ZEND_BEGIN_ARG_INFO_EX(color_setHex_args, 0, 0, 1)
ZEND_ARG_INFO(0, rgba)
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)
Expand Down Expand Up @@ -135,6 +152,121 @@ PHP_METHOD(colorObj, setRGB)
}
/* }}} */

/* {{{ 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_MAPSCRIPT_ERROR_HANDLING(TRUE);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll",
&red, &green, &blue, &alpha) == 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, alpha);

RETURN_LONG(MS_SUCCESS);
}
/* }}} */

/* {{{ proto int color.setHex(string hex)
Set new RGB(A) color from hex string. */
PHP_METHOD(colorObj, setHex)
{
zval *zobj = getThis();
char *hex;
long hex_len = 0, red, green, blue, alpha = 255;
php_color_object *php_color;

PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
&hex, &hex_len) == FAILURE) {
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
return;
}
PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);

if ((hex_len == 7 || hex_len == 9) && hex[0] == '#') {
red = msHexToInt(hex + 1);
green = msHexToInt(hex + 3);
blue = msHexToInt(hex + 5);
if (hex_len == 9) {
alpha = msHexToInt(hex + 7);
}

if (red > 255 || green > 255 || blue > 255 || alpha > 255) {
mapscript_throw_exception("Invalid color index." TSRMLS_CC);
RETURN_LONG(MS_FAILURE);
}

php_color = (php_color_object *) zend_object_store_get_object(zobj TSRMLS_CC);

MS_INIT_COLOR(*(php_color->color), red, green, blue, alpha);

RETURN_LONG(MS_SUCCESS);
} else {
mapscript_throw_exception("Invalid hex color string." TSRMLS_CC);
RETURN_LONG(MS_FAILURE);
}
}
/* }}} */

/* {{{ proto string color.toHex()
Get hex string #rrggbb. */
PHP_METHOD(colorObj, toHex)
{
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) {
mapscript_throw_exception("Can't express invalid color as hex." TSRMLS_CC);
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);
return;
}

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

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

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)
Expand Down
44 changes: 40 additions & 4 deletions mapscript/swiginc/color.i
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,35 @@
MS_INIT_COLOR(*self, red, green, blue, 255);
return MS_SUCCESS;
}

int setRGBA(int red, int green, int blue, int alpha)
{
/* Check colors */
if (red > 255 || green > 255 || blue > 255 || alpha > 255) {
msSetError(MS_MISCERR, "Invalid color index.", "setRGBA()");
return MS_FAILURE;
}

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

int setHex(char *psHexColor)
{
int red, green, blue;
if (psHexColor && strlen(psHexColor)== 7 && psHexColor[0] == '#') {
int red, green, blue, alpha = 255;
if (psHexColor && (strlen(psHexColor) == 7 || strlen(psHexColor) == 9) && psHexColor[0] == '#') {
red = msHexToInt(psHexColor+1);
green = msHexToInt(psHexColor+3);
blue= msHexToInt(psHexColor+5);
if (red > 255 || green > 255 || blue > 255) {
if (strlen(psHexColor) == 9) {
alpha = msHexToInt(psHexColor+7);
}
if (red > 255 || green > 255 || blue > 255 || alpha > 255) {
msSetError(MS_MISCERR, "Invalid color index.", "setHex()");
return MS_FAILURE;
}

MS_INIT_COLOR(*self, red, green, blue, 255);
MS_INIT_COLOR(*self, red, green, blue, alpha);
return MS_SUCCESS;
}
else {
Expand Down Expand Up @@ -116,5 +131,26 @@
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;
}
snprintf(hexcolor, 10, "#%02x%02x%02x%02x",
self->red, self->green, self->blue, self->alpha);
return strdup(hexcolor);
}
}

0 comments on commit 025ffe1

Please sign in to comment.