Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
lab.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include <cgraph/alloc.h>
12#include <cgraph/prisize_t.h>
13#include <sparse/general.h>
14#include <sparse/QuadTree.h>
15#include <edgepaint/lab.h>
16#include <math.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
21#include <edgepaint/lab_gamut.h>
22
23color_rgb color_rgb_init(double r, double g, double b){
24 color_rgb rgb;
25 rgb.r = r; rgb.g = g; rgb.b = b;
26 return rgb;
27}
28
29color_xyz color_xyz_init(double x, double y, double z){
30 color_xyz xyz;
31 xyz.x = x; xyz.y = y; xyz.z = z;
32 return xyz;
33}
34
35
36color_lab color_lab_init(double l, double a, double b){
37 color_lab lab;
38 lab.l = l; lab.a = a; lab.b = b;
39 return lab;
40}
41
42double XYZEpsilon = 216./24389.;
43double XYZKappa = 24389./27.;
44
45static double PivotXYZ(double n){
46 if (n > XYZEpsilon) return pow(n, 1/3.);
47 return (XYZKappa*n + 16)/116;
48}
49
50static double PivotRgb(double n){
51 if (n > 0.04045) return 100*pow((n + 0.055)/1.055, 2.4);
52 return 100*n/12.92;
53}
54
56 double r = PivotRgb(color.r/255.0);
57 double g = PivotRgb(color.g/255.0);
58 double b = PivotRgb(color.b/255.0);
59 return color_xyz_init(r*0.4124 + g*0.3576 + b*0.1805, r*0.2126 + g*0.7152 + b*0.0722, r*0.0193 + g*0.1192 + b*0.9505);
60}
61
63 color_xyz white = color_xyz_init(95.047, 100.000, 108.883);
64 color_xyz xyz = RGB2XYZ(color);
65 double x = PivotXYZ(xyz.x/white.x);
66 double y = PivotXYZ(xyz.y/white.y);
67 double z = PivotXYZ(xyz.z/white.z);
68 double L = MAX(0, 116*y - 16);
69 double A = 500*(x - y);
70 double B = 200*(y - z);
71 return color_lab_init(L, A, B);
72}
73
74void LAB2RGB_real_01(double *color){
75 /* convert an array[3] of LAB colors to RGB between 0 to 1, in place */
76 color_rgb rgb;
77 color_lab lab;
78
79 lab.l = color[0];
80 lab.a = color[1];
81 lab.b = color[2];
82 rgb = LAB2RGB(lab);
83 color[0] = rgb.r/255;
84 color[1] = rgb.g/255;
85 color[2] = rgb.b/255;
86}
88 double y = (color.l + 16.0)/116.0;
89 double x = color.a/500.0 + y;
90 double z = y - color.b/200.0;
91 color_xyz white = color_xyz_init(95.047, 100.000, 108.883), xyz;
92 double t1, t2, t3;
93 if(pow(x, 3.) > XYZEpsilon){
94 t1 = pow(x, 3.);
95 } else {
96 t1 = (x - 16.0/116.0)/7.787;
97 }
98 if (color.l > (XYZKappa*XYZEpsilon)){
99 t2 = pow(((color.l + 16.0)/116.0), 3.);
100 } else {
101 t2 = color.l/XYZKappa;
102 }
103 if (pow(z, 3.) > XYZEpsilon){
104 t3 = pow(z, 3.);
105 } else {
106 t3 = (z - 16.0/116.0)/7.787;
107 }
108 xyz = color_xyz_init(white.x*t1, white.y*t2, white.z*t3);
109 return XYZ2RGB(xyz);
110}
111
113 double x = color.x/100.0;
114 double y = color.y/100.0;
115 double z = color.z/100.0;
116 double r = x*3.2406 + y*(-1.5372) + z*(-0.4986);
117 double g = x*(-0.9689) + y*1.8758 + z*0.0415;
118 double b = x*0.0557 + y*(-0.2040) + z*1.0570;
119 if (r > 0.0031308){
120 r = 1.055*pow(r, 1/2.4) - 0.055;
121 } else {
122 r = 12.92*r;
123 }
124 if (g > 0.0031308) {
125 g = 1.055*pow(g, 1/2.4) - 0.055;
126 } else {
127 g = 12.92*g;
128 }
129 if (b > 0.0031308){
130 b = 1.055*pow(b, 1/2.4) - 0.055;
131 } else {
132 b = 12.92*b;
133 }
134 r = MAX(0, r);
135 r = MIN(255, r*255);
136 g = MAX(0, g);
137 g = MIN(255, g*255);
138 b = MAX(0, b);
139 b = MIN(255, b*255);
140
141 return color_rgb_init(r, g, b);
142}
143
144double *lab_gamut(const int *lightness, int *n) {
145 /* give a list of n points in the file defining the LAB color gamut.
146 */
147 double *xx, *x;
148
149 int l1 = lightness[0];
150 int l2 = lightness[1];
151
152 if (l1 < 0) l1 = 0;
153 if (l2 > 100) l2 = 100;
154 if (l1 > l2) l1 = l2;
155
156 if (Verbose)
157 fprintf(stderr,"LAB color lightness range = %d,%d\n", l1, l2);
158
159 if (Verbose)
160 fprintf(stderr,"size of lab gamut = %" PRISIZE_T "\n", lab_gamut_data_size);
161
162 // each L* value can be paired with 256 a* values and 256 b* values, so
163 // compute the maximum number of doubles we will need to span the space
164 size_t m = ((size_t)l2 - (size_t)l1 + 1) * 256 * 256 * 3;
165
166 x = malloc(sizeof(double)*m);
167 xx = x;
168 *n = 0;
169 for (size_t i = 0; i < lab_gamut_data_size; i += 4){
170 if (lab_gamut_data[i] >= l1 && lab_gamut_data[i] <= l2){
171 int b_lower = lab_gamut_data[i + 2];
172 int b_upper = lab_gamut_data[i + 3];
173 for (int b = b_lower; b <= b_upper; ++b) {
174 xx[0] = lab_gamut_data[i];
175 xx[1] = lab_gamut_data[i+1];
176 xx[2] = b;
177 xx += 3;
178 (*n)++;
179 }
180 }
181 }
182
183 return x;
184}
185
186QuadTree lab_gamut_quadtree(const int *lightness,
187 int max_qtree_level) {
188 /* read the color gamut points list in the form "x y z\n ..." and store in the octtree */
189 int n;
190 double *x = lab_gamut(lightness, &n);
191 QuadTree qt;
192 int dim = 3;
193
194 if (!x) return NULL;
195 qt = QuadTree_new_from_point_list(dim, n, max_qtree_level, x);
196
197
198 free(x);
199 return qt;
200}
201
202static double lab_dist(color_lab x, color_lab y){
203 return sqrt((x.l-y.l)*(x.l-y.l) +(x.a-y.a)*(x.a-y.a) +(x.b-y.b)*(x.b-y.b));
204}
205
206static void lab_interpolate(color_lab lab1, color_lab lab2, double t, double *colors){
207 colors[0] = lab1.l + t*(lab2.l - lab1.l);
208 colors[1] = lab1.a + t*(lab2.a - lab1.a);
209 colors[2] = lab1.b + t*(lab2.b - lab1.b);
210}
211
212double *color_blend_rgb2lab(char *color_list, const int maxpoints) {
213 /* give a color list of the form "#ff0000,#00ff00,...", get a list of around maxpoints
214 colors in an array colors0 of size [maxpoints*3] of the form {{l,a,b},...}.
215 color_list: either "#ff0000,#00ff00,...", or "pastel"
216 */
217
218 int nc = 1, r, g, b, i, ii, jj, cdim = 3;
219 char *cl;
220 color_rgb rgb;
221 double step, dist_current;
222 char *cp;
223
224 cp = color_palettes_get(color_list);
225 if (cp){
226 color_list = cp;
227 }
228
229 if (maxpoints <= 0) return NULL;
230
231 cl = color_list;
232 while ((cl=strchr(cl, ',')) != NULL){
233 cl++; nc++;
234 }
235 color_lab *lab = gv_calloc(MAX(nc, 1), sizeof(color_lab));
236
237 cl = color_list - 1;
238 nc = 0;
239 do {
240 cl++;
241 if (sscanf(cl,"#%02X%02X%02X", &r, &g, &b) != 3) break;
242 rgb.r = r; rgb.g = g; rgb.b = b;
243 lab[nc++] = RGB2LAB(rgb);
244 } while ((cl=strchr(cl, ',')) != NULL);
245
246 double *dists = gv_calloc(MAX(1, nc), sizeof(double));
247 dists[0] = 0;
248 for (i = 0; i < nc - 1; i++){
249 dists[i+1] = lab_dist(lab[i], lab[i+1]);
250 }
251 /* dists[i] is now the summed color distance from the 0-th color to the i-th color */
252 for (i = 0; i < nc - 1; i++){
253 dists[i+1] += dists[i];
254 }
255 if (Verbose)
256 fprintf(stderr,"sum = %f\n", dists[nc-1]);
257
258 double *colors = gv_calloc(maxpoints * cdim, sizeof(double));
259 if (maxpoints == 1){
260 colors[0] = lab[0].l;
261 colors[1] = lab[0].a;
262 colors[2] = lab[0].b;
263 } else {
264 step = dists[nc-1]/(maxpoints - 1);
265 ii = 0; jj = 0; dist_current = 0;
266 while (dists[jj] < dists[ii] + step) jj++;
267
268 double *colors_ptr = colors;
269 for (i = 0; i < maxpoints; i++){
270 lab_interpolate(lab[ii], lab[jj], (dist_current - dists[ii]) /
271 MAX(0.001, (dists[jj] - dists[ii])), colors_ptr);
272 dist_current += step;
273 colors_ptr += cdim;
274 if (dist_current > dists[jj]) ii = jj;
275 while (jj < nc -1 && dists[jj] < dists[ii] + step) jj++;
276 }
277 }
278 free(dists);
279 free(lab);
280 return colors;
281}
QuadTree QuadTree_new_from_point_list(int dim, int n, int max_level, double *coord)
Definition QuadTree.c:308
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define MIN(a, b)
Definition arith.h:28
char * color_palettes_get(char *color_palette_name)
#define A(n, t)
Definition expr.h:76
static int Verbose
Definition gml2gv.c:22
static Dt_t * L
Definition gmlparse.c:92
void * malloc(YYSIZE_T)
void free(void *)
node NULL
Definition grammar.y:149
static void color(Agraph_t *g)
Definition gvcolor.c:128
static int z
#define B
Definition hierarchy.c:117
QuadTree lab_gamut_quadtree(const int *lightness, int max_qtree_level)
construct a quadtree of the LAB gamut points
Definition lab.c:186
double * lab_gamut(const int *lightness, int *n)
Definition lab.c:144
color_xyz RGB2XYZ(color_rgb color)
Definition lab.c:55
void LAB2RGB_real_01(double *color)
Definition lab.c:74
static double PivotXYZ(double n)
Definition lab.c:45
color_rgb color_rgb_init(double r, double g, double b)
Definition lab.c:23
color_rgb XYZ2RGB(color_xyz color)
Definition lab.c:112
color_lab color_lab_init(double l, double a, double b)
Definition lab.c:36
static double PivotRgb(double n)
Definition lab.c:50
color_lab RGB2LAB(color_rgb color)
Definition lab.c:62
static double lab_dist(color_lab x, color_lab y)
Definition lab.c:202
color_xyz color_xyz_init(double x, double y, double z)
Definition lab.c:29
double XYZEpsilon
Definition lab.c:42
static void lab_interpolate(color_lab lab1, color_lab lab2, double t, double *colors)
Definition lab.c:206
double XYZKappa
Definition lab.c:43
color_rgb LAB2RGB(color_lab color)
Definition lab.c:87
double * color_blend_rgb2lab(char *color_list, const int maxpoints)
Definition lab.c:212
const signed char lab_gamut_data[]
Definition lab_gamut.c:14
const size_t lab_gamut_data_size
#define PRISIZE_T
PRIu64 alike for printing size_t
Definition prisize_t.h:27
double b
l: 0 to 100, a,b: -128 to 128
Definition lab.h:24
double a
Definition lab.h:24
double l
Definition lab.h:24
double r
Definition lab.h:14
double g
Definition lab.h:14
double b
Definition lab.h:14
double x
Definition lab.h:19
double z
Definition lab.h:19
double y
Definition lab.h:19
#define MAX(a, b)
Definition write.c:31