File: test-color-parser.c

package info (click to toggle)
gimp 3.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 210,076 kB
  • sloc: ansic: 842,287; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (120 lines) | stat: -rw-r--r-- 4,576 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#define DBL(c) ((gdouble)(c) / 255.0)
#define EPSILON 1e-6

typedef struct
{
  const gchar   *str;
  gboolean       fail;
  const gdouble  r;
  const gdouble  g;
  const gdouble  b;
  const gdouble  a;
} ColorSample;

static const ColorSample samples[] =
{
  /* sample                  fail   red       green     blue     alpha */

  { "#000000",               FALSE, 0.0,      0.0,      0.0,      1.0 },
  { "#FFff00",               FALSE, 1.0,      1.0,      0.0,      1.0 },
  { "#6495ed",               FALSE, DBL(100), DBL(149), DBL(237), 1.0 },
  { "#fff",                  FALSE, 1.0,      1.0,      1.0,      1.0 },
  /* Very unsure about this one. Our code has some support for values on 3 or 4
   * hexa per channel, but this doesn't exist in CSS specs.
   * On the other hand, we should add support for alpha.
   * And in any case, the result of the below should not be (1, 0, 0).
   * See: https://drafts.csswg.org/css-color/#hex-notation
   */
  /*{ "#64649595eded",         FALSE, 1.0,      1.0,      0.0,      1.0 },*/
  { "rgb(0,0,0)",            FALSE, 0.0,      0.0,      0.0,      1.0 },
  { "rgb(100,149,237)",      FALSE, DBL(100), DBL(149), DBL(237), 1.0 },
  { "rgba(100%,0,100%,0.5)", FALSE, 1.0,      0.0,      1.0,      0.5 },
  { "rgb(100%,149,20%)",     FALSE, 1.0,      DBL(149), 0.2,      1.0 },
  { "rgb(foobar)",           TRUE,  0.0,      0.0,      0.0,      1.0 },
  { "rgb(100,149,237",       TRUE,  0.0,      0.0,      0.0,      1.0 },
  { "rED",                   FALSE, 1.0,      0.0,      0.0,      1.0 },
  { "cornflowerblue",        FALSE, DBL(100), DBL(149), DBL(237), 1.0 },
  { "    red",               FALSE, 1.0,      0.0,      0.0,      1.0 },
  { "red      ",             FALSE, 1.0,      0.0,      0.0,      1.0 },
  { "red",                   FALSE, 1.0,      0.0,      0.0,      1.0 },
  { "red  blue",             TRUE,  0.0,      0.0,      0.0,      0.0 },
  { "transparent",           FALSE, 0.0,      0.0,      0.0,      0.0 },
  { "23foobar",              TRUE,  0.0,      0.0,      0.0,      0.0 },
  { "",                      TRUE,  0.0,      0.0,      0.0,      0.0 }
};

static gint
check_failure (const ColorSample *sample,
               GeglColor         *color)
{
  gdouble red;
  gdouble green;
  gdouble blue;
  gdouble alpha;

  if (color && sample->fail)
    {
      gegl_color_get_rgba_with_space (color, &red, &green, &blue, &alpha,
                                      babl_format ("R'G'B'A double"));
      g_printerr ("\n\tParser succeeded for sample \"%s\" but should have failed!\n"
                  "\t  parsed color: (%g, %g, %g, %g)\n",
                  sample->str, red, green, blue, alpha);
      return 1;
    }
  else if (! color && ! sample->fail)
    {
      g_printerr ("\n\tParser failed for sample \"%s\" but should have succeeded!\n"
                  "\t  expected color: (%g, %g, %g, %g)\n",
                  sample->str, sample->r, sample->g, sample->b, sample->a);
      return 1;
    }
  else if (! color && sample->fail)
    {
      return 0;
    }

  gegl_color_get_rgba_with_space (color, &red, &green, &blue, &alpha,
                                  babl_format ("R'G'B'A double"));
  if (fabs (red - sample->r) > EPSILON  || fabs (green - sample->g) > EPSILON ||
      fabs (blue - sample->b) > EPSILON || fabs (alpha - sample->a) > EPSILON)
    {
      g_printerr ("\nParser succeeded for sample \"%s\" but found the wrong values!\n"
                  "  parsed color:   (%g, %g, %g, %g)\n"
                  "  expected color: (%g, %g, %g, %g)\n",
                  sample->str, red, green, blue, alpha,
                  sample->r, sample->g, sample->b, sample->a);
      return 1;
    }

  return 0;
}

static GimpValueArray *
gimp_c_test_run (GimpProcedure        *procedure,
                 GimpRunMode           run_mode,
                 GimpImage            *image,
                 GimpDrawable        **drawables,
                 GimpProcedureConfig  *config,
                 gpointer              run_data)
{
  gint i;

  g_print ("\nTesting the GIMP color parser ...\n");

  for (i = 0; i < G_N_ELEMENTS (samples); i++)
    {
      GeglColor *color = NULL;
      gchar     *test;

      test = g_strdup_printf ("Parsing [%d] \"%s\" (should %s)",
                              i, samples[i].str,
                              samples[i].fail ? "fail" : "succeed");
      GIMP_TEST_START(test)
      color = gimp_color_parse_css (samples[i].str);

      GIMP_TEST_END(check_failure (samples + i, color) == 0)
      g_free (test);
    }

  GIMP_TEST_RETURN
}