24 int cdim,
double accuracy,
25 int seed,
double *colors,
27 double *color_diff_sum0) {
29 int i, j, *ia, *ja, n, k = 0;
35 double color_diff = 0, color_diff_old;
36 double color_diff_sum = 0, color_diff_sum_old, *cc;
38 static const int iter_max = 100;
39 double cspace_size = 0.7;
40 double red[3],
black[3], min;
44 max_level =
MAX(1, -log(accuracy)/log(2.));
45 max_level =
MIN(30, max_level);
48 color_rgb rgb = { .
r = 255*0.5, .g = 0, .b = 0 };
50 red[0] = lab.
l; red[1] = lab.
a; red[2] = lab.
b;
59 *color_diff0 = 1000; *color_diff_sum0 = 1000;
61 for (i = 0; i < cdim; i++) colors[i] = 0;
62 *color_diff0 = sqrt(cdim); *color_diff_sum0 = sqrt(cdim);
73 *color_diff0 = 1000; *color_diff_sum0 = 1000;
76 for (i = 0; i < cdim; i++) colors[i] = 0;
77 for (i = 0; i < cdim; i++) colors[cdim+i] = 0;
79 *color_diff0 = sqrt(cdim); *color_diff_sum0 = sqrt(cdim);
92 width = cspace_size*0.5;
96 for (i = 0; i < n*cdim; i++) colors[i] = cspace_size*
drand();
98 double *x =
gv_calloc(cdim * n,
sizeof(
double));
99 double *wgt = weightedQ ?
gv_calloc(n,
sizeof(
double)) :
NULL;
101 color_diff = 0; color_diff_old = -1;
102 color_diff_sum = 0; color_diff_sum_old = -1;
104 while (iter++ < iter_max && (color_diff > color_diff_old || (color_diff == color_diff_old && color_diff_sum > color_diff_sum_old))){
105 color_diff_old = color_diff;
106 color_diff_sum_old = color_diff_sum;
107 for (i = 0; i < n; i++){
109 for (j = ia[i]; j < ia[i+1]; j++){
110 if (ja[j] == i)
continue;
111 memcpy(&(x[k*cdim]), &(colors[ja[j]*cdim]),
sizeof(
double)*cdim);
112 if (wgt && a) wgt[k] = a[j];
115 cc = &(colors[i*cdim]);
124 color_diff = dist_max;
125 color_diff_sum = dist_max;
127 color_diff =
MIN(dist_max, color_diff);
128 color_diff_sum += dist_max;
132 if (
Verbose) fprintf(stderr,
"iter ---- %d ---, color_diff = %f, color_diff_sum = %f\n", iter, color_diff, color_diff_sum);
139 for (i = 0; i < n; i++){
140 lab =
color_lab_init(colors[i*cdim], colors[i*cdim+1], colors[i*cdim+2]);
142 colors[i*cdim] = (rgb.
r)/255;
143 colors[i*cdim+1] = (rgb.
g)/255;
144 colors[i*cdim+2] = (rgb.
b)/255;
147 *color_diff0 = color_diff;
148 *color_diff_sum0 = color_diff_sum;
155 int cdim,
double accuracy,
156 int seed,
double *colors) {
159 double color_diff_sum;
162 int iter, seed_max = -1;
163 double color_diff_max = -1;
166 for (i = 0; i < iter; i++){
169 if (color_diff_max < color_diff){
170 seed_max =
seed; color_diff_max = color_diff;
181 int seed,
int *cdim0,
double **colors) {
183 int ncomps, *comps =
NULL;
189 int maxcolors = 10000, max_qtree_level = 10, r, g, b;
191 if (color_list) color_scheme = color_list;
194 if (strcmp(color_scheme,
"lab") == 0){
195 if (
Verbose) fprintf(stderr,
"lab\n");
199 fprintf(stderr,
"out of memory\n");
202 }
else if (strcmp(color_scheme,
"rgb") == 0){
203 if (
Verbose) fprintf(stderr,
"rgb\n");
205 }
else if (strcmp(color_scheme,
"gray") == 0){
208 }
else if (sscanf(color_scheme,
"#%02X%02X%02X", &r, &g, &b) == 3 ){
211 assert(color_points);
221 if (accuracy <= 0) accuracy = 0.0001;
229 *colors =
gv_calloc(cdim * n,
sizeof(
double));
230 double *ctmp =
gv_calloc(cdim * n,
sizeof(
double));
237 for (i = 0; i < ncomps; i++){
238 nn = comps_ptr[i+1] - comps_ptr[i];
242 for (j = comps_ptr[i]; j < comps_ptr[i+1]; j++){
243 jj = j - comps_ptr[i];
244 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)