libgimpcolor: gimprgb-parse finally deleted.

- gimp_rgb_list_names() ported as gimp_color_list_names() using NUL-terminated
  GimpColorArray.
- GimpColorHexEntry uses the new function (and is therefore now GimpRGB free!).
- gimp_rgb_parse_name() deleted as it's unused since my previous commit.

With all this, gimprgb-parse.c is now deleted from the repository! \o/
This commit is contained in:
Jehan 2024-04-20 19:58:11 +02:00
parent b31a465a9e
commit d661e8efb1
8 changed files with 79 additions and 383 deletions

View File

@ -29,6 +29,8 @@
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include "libgimpbase/gimpbase.h"
#include "gimpcolor.h"
@ -257,6 +259,55 @@ gimp_color_parse_name (const gchar *name)
return gimp_color_parse_name_substring (name, -1);
}
/**
* gimp_color_list_names:
* @colors: (out) (optional) (array zero-terminated=1) (element-type GeglColor) (transfer full): return location for an array of [class@Gegl.Color]
*
* Returns the list of [SVG 1.0 color
* keywords](https://www.w3.org/TR/SVG/types.html) that is recognized by
* [func@color_parse_name].
*
* The returned strings are const and must not be freed. Only the array
* must be freed with `g_free()`.
*
* The optional @colors arrays must be freed with [func@color_array_free] when
* they are no longer needed.
*
* Returns: (array zero-terminated=1) (transfer container): an array of color names.
*
* Since: 2.2
**/
const gchar **
gimp_color_list_names (GimpColorArray *colors)
{
const gchar **names;
gint i;
names = g_new0 (const gchar *, G_N_ELEMENTS (named_colors) + 1);
if (colors)
*colors = g_new0 (GeglColor *, G_N_ELEMENTS (named_colors) + 1);
for (i = 0; i < G_N_ELEMENTS (named_colors); i++)
{
names[i] = named_colors[i].name;
if (colors)
{
GeglColor *color = gegl_color_new (NULL);
gegl_color_set_rgba_with_space (color,
(gdouble) named_colors[i].red / 255.0,
(gdouble) named_colors[i].green / 255.0,
(gdouble) named_colors[i].blue / 255.0,
1.0, NULL);
(*colors)[i] = color;
}
}
return names;
}
/**
* gimp_color_parse_css_substring: (skip)
* @css: (array length=len): a string describing a color in CSS notation

View File

@ -28,6 +28,8 @@
#include <babl/babl.h>
#include <gegl.h>
#include "libgimpbase/gimpbase.h"
#include "gimpcolor.h"

View File

@ -22,6 +22,7 @@ EXPORTS
gimp_color_is_out_of_gamut
gimp_color_is_out_of_self_gamut
gimp_color_is_perceptually_identical
gimp_color_list_names
gimp_color_managed_get_color_profile
gimp_color_managed_get_icc_profile
gimp_color_managed_get_simulation_bpc
@ -96,13 +97,11 @@ EXPORTS
gimp_rgb_composite
gimp_rgb_get_type
gimp_rgb_get_uchar
gimp_rgb_list_names
gimp_rgb_luminance
gimp_rgb_luminance_uchar
gimp_rgb_max
gimp_rgb_min
gimp_rgb_multiply
gimp_rgb_parse_name
gimp_rgb_set
gimp_rgb_set_alpha
gimp_rgb_set_uchar

View File

@ -21,6 +21,8 @@
#define __GIMP_COLOR_H_INSIDE__
#include <libgimpbase/gimpbase.h>
#include <libgimpcolor/gimpcolortypes.h>
#include <libgimpcolor/gimpadaptivesupersample.h>
@ -47,26 +49,27 @@ G_BEGIN_DECLS
#define GIMP_VALUE_HOLDS_COLOR(value) (G_TYPE_CHECK_VALUE_TYPE ((value), GEGL_TYPE_COLOR))
void gimp_color_set_alpha (GeglColor *color,
gdouble alpha);
void gimp_color_set_alpha (GeglColor *color,
gdouble alpha);
gboolean gimp_color_is_perceptually_identical (GeglColor *color1,
GeglColor *color2);
gboolean gimp_color_is_perceptually_identical (GeglColor *color1,
GeglColor *color2);
GeglColor * gimp_color_parse_css (const gchar *css);
GeglColor * gimp_color_parse_hex (const gchar *hex);
GeglColor * gimp_color_parse_name (const gchar *name);
GeglColor * gimp_color_parse_css (const gchar *css);
GeglColor * gimp_color_parse_hex (const gchar *hex);
GeglColor * gimp_color_parse_name (const gchar *name);
const gchar ** gimp_color_list_names (GimpColorArray *colors);
GeglColor * gimp_color_parse_css_substring (const gchar *css,
gint len);
GeglColor * gimp_color_parse_hex_substring (const gchar *hex,
gint len);
GeglColor * gimp_color_parse_name_substring (const gchar *name,
gint len);
GeglColor * gimp_color_parse_css_substring (const gchar *css,
gint len);
GeglColor * gimp_color_parse_hex_substring (const gchar *hex,
gint len);
GeglColor * gimp_color_parse_name_substring (const gchar *name,
gint len);
gboolean gimp_color_is_out_of_self_gamut (GeglColor *color);
gboolean gimp_color_is_out_of_gamut (GeglColor *color,
const Babl *space);
gboolean gimp_color_is_out_of_self_gamut (GeglColor *color);
gboolean gimp_color_is_out_of_gamut (GeglColor *color,
const Babl *space);
/*
* GIMP_TYPE_PARAM_COLOR

View File

@ -1,344 +0,0 @@
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimprgb-parse.c
* Copyright (C) 2004 Sven Neumann <sven@gimp.org>
*
* Some of the code in here was inspired and partly copied from pango
* and librsvg.
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <babl/babl.h>
#include <glib-object.h>
#include "gimpcolortypes.h"
#include "gimpcolorspace.h"
#include "gimprgb.h"
static gchar * gimp_rgb_parse_strip (const gchar *str,
gint len);
static gboolean gimp_rgb_parse_name_internal (GimpRGB *rgb,
const gchar *name);
typedef struct
{
const gchar *name;
const guchar red;
const guchar green;
const guchar blue;
} ColorEntry;
static const ColorEntry named_colors[] =
{
{ "aliceblue", 240, 248, 255 },
{ "antiquewhite", 250, 235, 215 },
{ "aqua", 0, 255, 255 },
{ "aquamarine", 127, 255, 212 },
{ "azure", 240, 255, 255 },
{ "beige", 245, 245, 220 },
{ "bisque", 255, 228, 196 },
{ "black", 0, 0, 0 },
{ "blanchedalmond", 255, 235, 205 },
{ "blue", 0, 0, 255 },
{ "blueviolet", 138, 43, 226 },
{ "brown", 165, 42, 42 },
{ "burlywood", 222, 184, 135 },
{ "cadetblue", 95, 158, 160 },
{ "chartreuse", 127, 255, 0 },
{ "chocolate", 210, 105, 30 },
{ "coral", 255, 127, 80 },
{ "cornflowerblue", 100, 149, 237 },
{ "cornsilk", 255, 248, 220 },
{ "crimson", 220, 20, 60 },
{ "cyan", 0, 255, 255 },
{ "darkblue", 0, 0, 139 },
{ "darkcyan", 0, 139, 139 },
{ "darkgoldenrod", 184, 134, 11 },
{ "darkgray", 169, 169, 169 },
{ "darkgreen", 0, 100, 0 },
{ "darkgrey", 169, 169, 169 },
{ "darkkhaki", 189, 183, 107 },
{ "darkmagenta", 139, 0, 139 },
{ "darkolivegreen", 85, 107, 47 },
{ "darkorange", 255, 140, 0 },
{ "darkorchid", 153, 50, 204 },
{ "darkred", 139, 0, 0 },
{ "darksalmon", 233, 150, 122 },
{ "darkseagreen", 143, 188, 143 },
{ "darkslateblue", 72, 61, 139 },
{ "darkslategray", 47, 79, 79 },
{ "darkslategrey", 47, 79, 79 },
{ "darkturquoise", 0, 206, 209 },
{ "darkviolet", 148, 0, 211 },
{ "deeppink", 255, 20, 147 },
{ "deepskyblue", 0, 191, 255 },
{ "dimgray", 105, 105, 105 },
{ "dimgrey", 105, 105, 105 },
{ "dodgerblue", 30, 144, 255 },
{ "firebrick", 178, 34, 34 },
{ "floralwhite" , 255, 250, 240 },
{ "forestgreen", 34, 139, 34 },
{ "fuchsia", 255, 0, 255 },
{ "gainsboro", 220, 220, 220 },
{ "ghostwhite", 248, 248, 255 },
{ "gold", 255, 215, 0 },
{ "goldenrod", 218, 165, 32 },
{ "gray", 128, 128, 128 },
{ "green", 0, 128, 0 },
{ "greenyellow", 173, 255, 47 },
{ "grey", 128, 128, 128 },
{ "honeydew", 240, 255, 240 },
{ "hotpink", 255, 105, 180 },
{ "indianred", 205, 92, 92 },
{ "indigo", 75, 0, 130 },
{ "ivory", 255, 255, 240 },
{ "khaki", 240, 230, 140 },
{ "lavender", 230, 230, 250 },
{ "lavenderblush", 255, 240, 245 },
{ "lawngreen", 124, 252, 0 },
{ "lemonchiffon", 255, 250, 205 },
{ "lightblue", 173, 216, 230 },
{ "lightcoral", 240, 128, 128 },
{ "lightcyan", 224, 255, 255 },
{ "lightgoldenrodyellow", 250, 250, 210 },
{ "lightgray", 211, 211, 211 },
{ "lightgreen", 144, 238, 144 },
{ "lightgrey", 211, 211, 211 },
{ "lightpink", 255, 182, 193 },
{ "lightsalmon", 255, 160, 122 },
{ "lightseagreen", 32, 178, 170 },
{ "lightskyblue", 135, 206, 250 },
{ "lightslategray", 119, 136, 153 },
{ "lightslategrey", 119, 136, 153 },
{ "lightsteelblue", 176, 196, 222 },
{ "lightyellow", 255, 255, 224 },
{ "lime", 0, 255, 0 },
{ "limegreen", 50, 205, 50 },
{ "linen", 250, 240, 230 },
{ "magenta", 255, 0, 255 },
{ "maroon", 128, 0, 0 },
{ "mediumaquamarine", 102, 205, 170 },
{ "mediumblue", 0, 0, 205 },
{ "mediumorchid", 186, 85, 211 },
{ "mediumpurple", 147, 112, 219 },
{ "mediumseagreen", 60, 179, 113 },
{ "mediumslateblue", 123, 104, 238 },
{ "mediumspringgreen", 0, 250, 154 },
{ "mediumturquoise", 72, 209, 204 },
{ "mediumvioletred", 199, 21, 133 },
{ "midnightblue", 25, 25, 112 },
{ "mintcream", 245, 255, 250 },
{ "mistyrose", 255, 228, 225 },
{ "moccasin", 255, 228, 181 },
{ "navajowhite", 255, 222, 173 },
{ "navy", 0, 0, 128 },
{ "oldlace", 253, 245, 230 },
{ "olive", 128, 128, 0 },
{ "olivedrab", 107, 142, 35 },
{ "orange", 255, 165, 0 },
{ "orangered", 255, 69, 0 },
{ "orchid", 218, 112, 214 },
{ "palegoldenrod", 238, 232, 170 },
{ "palegreen", 152, 251, 152 },
{ "paleturquoise", 175, 238, 238 },
{ "palevioletred", 219, 112, 147 },
{ "papayawhip", 255, 239, 213 },
{ "peachpuff", 255, 218, 185 },
{ "peru", 205, 133, 63 },
{ "pink", 255, 192, 203 },
{ "plum", 221, 160, 221 },
{ "powderblue", 176, 224, 230 },
{ "purple", 128, 0, 128 },
{ "red", 255, 0, 0 },
{ "rosybrown", 188, 143, 143 },
{ "royalblue", 65, 105, 225 },
{ "saddlebrown", 139, 69, 19 },
{ "salmon", 250, 128, 114 },
{ "sandybrown", 244, 164, 96 },
{ "seagreen", 46, 139, 87 },
{ "seashell", 255, 245, 238 },
{ "sienna", 160, 82, 45 },
{ "silver", 192, 192, 192 },
{ "skyblue", 135, 206, 235 },
{ "slateblue", 106, 90, 205 },
{ "slategray", 112, 128, 144 },
{ "slategrey", 112, 128, 144 },
{ "snow", 255, 250, 250 },
{ "springgreen", 0, 255, 127 },
{ "steelblue", 70, 130, 180 },
{ "tan", 210, 180, 140 },
{ "teal", 0, 128, 128 },
{ "thistle", 216, 191, 216 },
{ "tomato", 255, 99, 71 },
{ "turquoise", 64, 224, 208 },
{ "violet", 238, 130, 238 },
{ "wheat", 245, 222, 179 },
{ "white", 255, 255, 255 },
{ "whitesmoke", 245, 245, 245 },
{ "yellow", 255, 255, 0 },
{ "yellowgreen", 154, 205, 50 }
};
/**
* gimp_rgb_parse_name:
* @rgb: a #GimpRGB struct used to return the parsed color
* @name: (array length=len): a color name (in UTF-8 encoding)
* @len: the length of @name, in bytes. or -1 if @name is nul-terminated
*
* Attempts to parse a color name. This function accepts <ulink
* url="https://www.w3.org/TR/SVG/types.html#ColorKeywords">SVG 1.0
* color keywords</ulink>.
*
* This function does not touch the alpha component of @rgb.
*
* Returns: %TRUE if @name was parsed successfully and @rgb has
* been set, %FALSE otherwise
*
* Since: 2.2
**/
gboolean
gimp_rgb_parse_name (GimpRGB *rgb,
const gchar *name,
gint len)
{
gchar *tmp;
gboolean result;
g_return_val_if_fail (rgb != NULL, FALSE);
g_return_val_if_fail (name != NULL, FALSE);
tmp = gimp_rgb_parse_strip (name, len);
result = gimp_rgb_parse_name_internal (rgb, tmp);
g_free (tmp);
return result;
}
/**
* gimp_rgb_list_names:
* @names: (out) (array length=n_colors) (transfer container): return location for an array of color names
* @colors: (out) (array length=n_colors) (transfer container): return location for an array of GimpRGB structs
* @n_colors: (out): The number of named colors
*
* Returns the list of <ulink
* url="https://www.w3.org/TR/SVG/types.html">SVG 1.0 color
* keywords</ulink> that is used by gimp_rgb_parse_name().
*
* The returned strings are const and must not be freed. Only the two
* arrays are allocated dynamically. You must call g_free() on the
* @names and @colors arrays when they are not any longer needed.
*
* Since: 2.2
**/
void
gimp_rgb_list_names (const gchar ***names,
GimpRGB **colors,
gint *n_colors)
{
gint i;
g_return_if_fail (names != NULL);
g_return_if_fail (colors != NULL);
g_return_if_fail (n_colors != NULL);
*names = g_new (const gchar *, G_N_ELEMENTS (named_colors));
*colors = g_new (GimpRGB, G_N_ELEMENTS (named_colors));
*n_colors = G_N_ELEMENTS (named_colors);
for (i = 0; i < G_N_ELEMENTS (named_colors); i++)
{
(*names)[i] = named_colors[i].name;
gimp_rgba_set_uchar ((*colors) + i,
named_colors[i].red,
named_colors[i].green,
named_colors[i].blue,
0xFF);
}
}
static gchar *
gimp_rgb_parse_strip (const gchar *str,
gint len)
{
gchar *result;
while (len > 0 && g_ascii_isspace (*str))
{
str++;
len--;
}
if (len < 0)
{
while (g_ascii_isspace (*str))
str++;
len = strlen (str);
}
while (len > 0 && g_ascii_isspace (str[len - 1]))
len--;
result = g_malloc (len + 1);
memcpy (result, str, len);
result[len] = '\0';
return result;
}
static gint
gimp_rgb_color_entry_compare (gconstpointer a,
gconstpointer b)
{
const gchar *name = a;
const ColorEntry *entry = b;
return g_ascii_strcasecmp (name, entry->name);
}
static gboolean
gimp_rgb_parse_name_internal (GimpRGB *rgb,
const gchar *name)
{
ColorEntry *entry = bsearch (name, named_colors,
G_N_ELEMENTS (named_colors), sizeof (ColorEntry),
gimp_rgb_color_entry_compare);
if (entry)
{
gimp_rgb_set_uchar (rgb, entry->red, entry->green, entry->blue);
return TRUE;
}
return FALSE;
}

View File

@ -69,9 +69,6 @@ void gimp_rgb_get_uchar (const GimpRGB *rgb,
guchar *green,
guchar *blue);
gboolean gimp_rgb_parse_name (GimpRGB *rgb,
const gchar *name,
gint len);
void gimp_rgb_add (GimpRGB *rgb1,
const GimpRGB *rgb2);
void gimp_rgb_multiply (GimpRGB *rgb1,
@ -88,12 +85,6 @@ void gimp_rgb_composite (GimpRGB *color1,
const GimpRGB *color2,
GimpRGBCompositeMode mode);
/* access to the list of color names */
void gimp_rgb_list_names (const gchar ***names,
GimpRGB **colors,
gint *n_colors);
void gimp_rgba_set (GimpRGB *rgba,
gdouble red,
gdouble green,

View File

@ -12,7 +12,6 @@ libgimpcolor_sources = files(
'gimphsl.c',
'gimphsv.c',
'gimppixbuf.c',
'gimprgb-parse.c',
'gimprgb.c',
)

View File

@ -116,9 +116,8 @@ gimp_color_hex_entry_init (GimpColorHexEntry *entry)
GtkEntryCompletion *completion;
GtkCellRenderer *cell;
GtkListStore *store;
GimpRGB *colors;
GeglColor **colors;
const gchar **names;
gint num_colors;
gint i;
entry->priv = gimp_color_hex_entry_get_instance_private (entry);
@ -139,25 +138,21 @@ gimp_color_hex_entry_init (GimpColorHexEntry *entry)
store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, GEGL_TYPE_COLOR);
gimp_rgb_list_names (&names, &colors, &num_colors);
names = gimp_color_list_names (&colors);
for (i = 0; i < num_colors; i++)
for (i = 0; names[i] != NULL; i++)
{
GtkTreeIter iter;
GeglColor *named_color = gegl_color_new ("black");
gegl_color_set_rgba_with_space (named_color, colors[i].r, colors[i].g,
colors[i].b, colors[i].a, NULL);
GeglColor *named_color = colors[i];
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
COLUMN_NAME, names[i],
COLUMN_COLOR, named_color,
-1);
g_object_unref (named_color);
}
g_free (colors);
gimp_color_array_free (colors);
g_free (names);
completion = g_object_new (GTK_TYPE_ENTRY_COMPLETION,
@ -222,7 +217,7 @@ gimp_color_hex_entry_new (void)
/**
* gimp_color_hex_entry_set_color:
* @entry: a #GimpColorHexEntry widget
* @color: pointer to a #GimpRGB
* @color: the color to set.
*
* Sets the color displayed by a #GimpColorHexEntry. If the new color
* is different to the previously set color, the "color-changed"