25 int cdim,
double accuracy,
26 int seed,
double *colors,
28 double *color_diff_sum0) {
30 int i, j, *ia, *ja, n, k = 0;
36 double color_diff = 0, color_diff_old;
37 double color_diff_sum = 0, color_diff_sum_old, *cc;
39 static const int iter_max = 100;
40 double cspace_size = 0.7;
41 double red[3],
black[3], min;
45 max_level =
MAX(1, -log(accuracy)/log(2.));
46 max_level =
MIN(30, max_level);
49 color_rgb rgb = { .
r = 255*0.5, .g = 0, .b = 0 };
51 red[0] = lab.
l; red[1] = lab.
a; red[2] = lab.
b;
60 *color_diff0 = 1000; *color_diff_sum0 = 1000;
62 for (i = 0; i < cdim; i++) colors[i] = 0;
63 *color_diff0 = sqrt(cdim); *color_diff_sum0 = sqrt(cdim);
74 *color_diff0 = 1000; *color_diff_sum0 = 1000;
77 for (i = 0; i < cdim; i++) colors[i] = 0;
78 for (i = 0; i < cdim; i++) colors[cdim+i] = 0;
80 *color_diff0 = sqrt(cdim); *color_diff_sum0 = sqrt(cdim);
93 width = cspace_size*0.5;
97 for (i = 0; i < n*cdim; i++) colors[i] = cspace_size*
drand();
99 double *x =
gv_calloc(cdim * n,
sizeof(
double));
100 double *wgt = weightedQ ?
gv_calloc(n,
sizeof(
double)) :
NULL;
102 color_diff = 0; color_diff_old = -1;
103 color_diff_sum = 0; color_diff_sum_old = -1;
105 while (iter++ < iter_max && (color_diff > color_diff_old || (color_diff == color_diff_old && color_diff_sum > color_diff_sum_old))){
106 color_diff_old = color_diff;
107 color_diff_sum_old = color_diff_sum;
108 for (i = 0; i < n; i++){
110 for (j = ia[i]; j < ia[i+1]; j++){
111 if (ja[j] == i)
continue;
112 memcpy(&(x[k*cdim]), &(colors[ja[j]*cdim]),
sizeof(
double)*cdim);
113 if (wgt && a) wgt[k] = a[j];
116 cc = &(colors[i*cdim]);
125 color_diff = dist_max;
126 color_diff_sum = dist_max;
128 color_diff =
MIN(dist_max, color_diff);
129 color_diff_sum += dist_max;
133 if (
Verbose) fprintf(stderr,
"iter ---- %d ---, color_diff = %f, color_diff_sum = %f\n", iter, color_diff, color_diff_sum);
140 for (i = 0; i < n; i++){
141 lab =
color_lab_init(colors[i*cdim], colors[i*cdim+1], colors[i*cdim+2]);
143 colors[i*cdim] = (rgb.
r)/255;
144 colors[i*cdim+1] = (rgb.
g)/255;
145 colors[i*cdim+2] = (rgb.
b)/255;
148 *color_diff0 = color_diff;
149 *color_diff_sum0 = color_diff_sum;
156 int cdim,
double accuracy,
157 int seed,
double *colors) {
160 double color_diff_sum;
163 int iter, seed_max = -1;
164 double color_diff_max = -1;
167 for (i = 0; i < iter; i++){
170 if (color_diff_max < color_diff){
171 seed_max =
seed; color_diff_max = color_diff;
182 int seed,
int *cdim0,
double **colors) {
184 int ncomps, *comps =
NULL;
190 int maxcolors = 10000, max_qtree_level = 10, r, g, b;
192 if (color_list) color_scheme = color_list;
195 if (strcmp(color_scheme,
"lab") == 0){
196 if (
Verbose) fprintf(stderr,
"lab\n");
200 fprintf(stderr,
"out of memory\n");
203 }
else if (strcmp(color_scheme,
"rgb") == 0){
204 if (
Verbose) fprintf(stderr,
"rgb\n");
206 }
else if (strcmp(color_scheme,
"gray") == 0){
209 }
else if (sscanf(color_scheme,
"#%02X%02X%02X", &r, &g, &b) == 3 ){
212 assert(color_points);
222 if (accuracy <= 0) accuracy = 0.0001;
230 *colors =
gv_calloc(cdim * n,
sizeof(
double));
231 double *ctmp =
gv_calloc(cdim * n,
sizeof(
double));
238 for (i = 0; i < ncomps; i++){
239 nn = comps_ptr[i+1] - comps_ptr[i];
243 for (j = comps_ptr[i]; j < comps_ptr[i+1]; j++){
244 jj = j - comps_ptr[i];
245 memcpy(&((*colors)[comps[j]*cdim]), &(ctmp[jj*cdim]), cdim*
sizeof(
double));
void QuadTree_get_nearest(QuadTree qt, double *x, double *ymin, int *imin, double *min)
QuadTree QuadTree_new_from_point_list(int dim, int n, int max_level, double *coord)
void QuadTree_delete(QuadTree q)
SparseMatrix SparseMatrix_get_submatrix(SparseMatrix A, int nrow, int ncol, int *rindices, int *cindices)
SparseMatrix SparseMatrix_symmetrize(SparseMatrix A, bool pattern_symmetric_only)
void SparseMatrix_delete(SparseMatrix A)
int * SparseMatrix_weakly_connected_components(SparseMatrix A0, int *ncomp, int **comps)
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
const char * color_palettes_get(const char *color_palette_name)
void furtherest_point_in_list(int k, int dim, double *wgt, double *pts, QuadTree qt, int max_level, double *dist_max, double **argmax)
void furtherest_point(int k, int dim, double *wgt, double *pts, double *center, double width, int max_level, double *dist_max, double **argmax)
static double drand(void)
static int imin(int a, int b)
minimum of two integers
static const char black[]
QuadTree lab_gamut_quadtree(const int *lightness, int max_qtree_level)
construct a quadtree of the LAB gamut points
void LAB2RGB_real_01(double *color)
color_lab color_lab_init(double l, double a, double b)
color_lab RGB2LAB(color_rgb color)
double * color_blend_rgb2lab(const char *color_list, const int maxpoints)
color_rgb LAB2RGB(color_lab color)
static void node_distinct_coloring_internal(int scheme, QuadTree qt, bool weightedQ, SparseMatrix A, int cdim, double accuracy, int seed, double *colors)
int node_distinct_coloring(const char *color_scheme, int *lightness, bool weightedQ, SparseMatrix A0, double accuracy, int seed, int *cdim0, double **colors)
static void node_distinct_coloring_internal2(int scheme, QuadTree qt, bool weightedQ, SparseMatrix A, int cdim, double accuracy, int seed, double *colors, double *color_diff0, double *color_diff_sum0)
double b
l: 0 to 100, a,b: -128 to 128
static point center(point vertex[], size_t n)