Changeset 69708


Ignore:
Timestamp:
Oct 19, 2016, 3:17:34 PM (8 years ago)
Author:
wenzeslaus
Message:

rasterlib: use G_str_to_color() for parsing color table rules

This introduces whatever syntax from G_str_to_color() to Rast_parse_color_rule(),
i.e. not only 'v r g b', 'v r:g:b', and 'v name' is supported, but also any other
separator from G_str_to_color() which now adds comma and semicolon.

Also hexadecimal colors from r69683 are now supported which makes the fact
that comments in color rules (indicated by hash) must start at the first column (character)
more important than before. (Comments starting in the middle of the line are not
currently supported, but from now on they cannot be supported because it would interfere
with color identification because hexadecimal colors are (usually and in G_str_to_color())
indicated by hash.)

Now also NONE/none color is parsed but considered as an syntax error and not reported
separately to the user.

Number of errors (error strings) reported to user was reduced because G_str_to_color()
has limited diagnostics. Specifically, out-of-range RGB values and broken syntax (which
includes and was not distinguished from wrong color names) is reported and syntax
error in color (if the paring of the whole rule was successful).

The commit removes last/only usage of the G_color_values() function. Named
colors for Rast_parse_color_rule() now go from the lib/gis/color_str.c file and
not from the lib/gis/named_colr.c file.

Location:
grass/trunk/lib/raster
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • grass/trunk/lib/raster/color_rules.c

    r57540 r69708  
    11/*!
    2   \file lib/raster/color_read.c
     2  \file lib/raster/color_rules.c
    33 
    44  \brief Raster Library - Read and parse color rules file
    55 
    6   (C) 2007 by the GRASS Development Team
     6  (C) 2007-2016 by the GRASS Development Team
    77 
    88  This program is free software under the GNU General Public
     
    1616
    1717#include <grass/gis.h>
     18#include <grass/colors.h>
    1819#include <grass/raster.h>
    1920#include <grass/glocale.h>
     
    2930{
    3031    CR_OK = 0,
    31     CR_ERROR_SYNTAX,
    32     CR_ERROR_RGB,
    33     CR_ERROR_COLOR,
     32    CR_ERROR_RULE_SYNTAX,
     33    CR_ERROR_COLOR_SYNTAX,
    3434    CR_ERROR_PERCENT,
    3535    CR_ERROR_VALUE,
     
    3939  \brief Read color rule
    4040
    41   \param min, max min & max values (used only when color rules are in percentage)
    42   \param buf
    43   \param val value
     41  The val output parameter is always an absolute value and is derived
     42  from the rule and the min and max values in case the rule is in percents.
     43
     44  Always only one of the norm, nval, and dflt output parameters is set
     45  to non-zero value, the others are set to zero.
     46
     47  The return code can be translated to an error message using
     48  the Rast_parse_color_rule_error() function.
     49
     50  \param min, max min & max values (used only when color rules are in percentage)
     51  \param buf string with the color rule
     52  \param[out] val value which the color is assigned to
    4453  \param[out] r,g,b color values
    45   \param norm
    46   \param nval
    47   \param dflt
    48 
    49   \return 0 on failure
    50   \return 1 on success
     54  \param[out] norm set to non-zero value if the value and color are set
     55  \param[out] nval set to non-zero value if rule is for null value
     56  \param[out] dflt set to non-zero value if rule specifies the default color
     57
     58  \returns enum rule_error values (non-zero on failure)
    5159*/
    5260int Rast_parse_color_rule(DCELL min, DCELL max, const char *buf,
     
    6169
    6270    if (sscanf(buf, "%s %[^\n]", value, color) != 2)
    63         return CR_ERROR_SYNTAX;
    64 
    65     G_chop(color);
    66 
    67     if (sscanf(color, "%d:%d:%d", r, g, b) == 3 ||
    68         sscanf(color, "%d %d %d", r, g, b) == 3) {
    69         if (*r < 0 || *r > 255 || *g < 0 || *g > 255 || *b < 0 || *b > 255)
    70             return CR_ERROR_RGB;
    71     }
    72     else {
    73         float fr, fg, fb;
    74 
    75         if (G_color_values(color, &fr, &fg, &fb) < 0)
    76             return CR_ERROR_COLOR;
    77 
    78         *r = (int)(fr * 255.99);
    79         *g = (int)(fg * 255.99);
    80         *b = (int)(fb * 255.99);
    81     }
     71        return CR_ERROR_RULE_SYNTAX;
     72
     73    /* we don't allow 'none' color here (ret == 2) */
     74    /* G_str_to_color chops and has only 1 error state */
     75    if (G_str_to_color(color, r, g, b) != 1)
     76            return CR_ERROR_COLOR_SYNTAX;
    8277
    8378    G_chop(value);
     
    122117    case CR_OK:
    123118        return "";
    124     case CR_ERROR_SYNTAX:
    125         return _("syntax error");
     119    case CR_ERROR_RULE_SYNTAX:
     120        return _("syntax error in the color rule");
     121    case CR_ERROR_COLOR_SYNTAX:
     122        return _("syntax error in the color format");
     123/* we no longer distinguish between these two errors
    126124    case CR_ERROR_RGB:
    127125        return _("R/G/B not in range 0-255");
    128126    case CR_ERROR_COLOR:
    129127        return _("invalid color name");
     128*/
    130129    case CR_ERROR_PERCENT:
    131130        return _("percentage not in range 0-100");
Note: See TracChangeset for help on using the changeset viewer.