/* GNUPLOT - getcolor.c */
/*[
*
* Petr Mikulik, December 1998 -- June 1999
* Copyright: open source as much as possible
*
]*/
#include "syscfg.h"
#include "stdfn.h"
#include "util.h"
#include "getcolor.h"
/*
* This file implements all the gray to color transforms except one:
* calculate_color_from_formulae() which can be found in color.c.
* calculate_color_from_formulae() belongs logicaly into this (getcolor.c)
* file, but it can't be here due to linkage problems: gnuplot_x11
* needs a lot of code from getcolor.c but does not contain any function
* evaluation code [and does not call calculate_color_from_formulae()].
* This file is compiled twice: Once as part of gnuplot and the second
* time with -DGPLT_X11_MODE resulting in getcolor_x11.o which is linked
* into gnuplot_x11. With GPLT_X11_MODE defined this file does not
* contain code for calculating colors from gray by user defined functions.
*/
#ifndef GPLT_X11_MODE
static int calculate_color_from_formulae(double, rgb_color *);
#endif
static void color_components_from_gray(double gray, rgb_color *color);
static int interpolate_color_from_gray(double, rgb_color *);
static double get_max_dev(rgb_color *colors, int j, double limit);
static int is_extremum(rgb_color left,rgb_color mid,rgb_color right);
static void CMY_2_RGB(rgb_color *color);
static void HSV_2_RGB(rgb_color *color);
/* check if two palettes p1 and p2 differ significantly */
int
palettes_differ(t_sm_palette *p1, t_sm_palette *p2)
{
if (p1->colorMode != p2->colorMode)
return 1;
if (p1->positive != p2->positive)
return 1;
if (p1->cmodel != p2->cmodel)
return 1;
if (p1->use_maxcolors != p2->use_maxcolors)
return 1;
switch(p1->colorMode) {
case SMPAL_COLOR_MODE_NONE:
return 0; /* ?? */
case SMPAL_COLOR_MODE_GRAY:
if (fabs(p1->gamma - p2->gamma) > 1e-3)
return 1;
break;
case SMPAL_COLOR_MODE_RGB:
if (p1->colorFormulae != p2->colorFormulae)
return 1;
if (p1->formulaR != p2->formulaR)
return 1;
if (p1->formulaG != p2->formulaG)
return 1;
if (p1->formulaB != p2->formulaB)
return 1;
/* if (p1->ps_allcF != p2->ps_allcF)
return 1; */
break;
case SMPAL_COLOR_MODE_FUNCTIONS:
/* coarse check based on typed fnct definitions */
if (strcmp(p1->Afunc.definition, p2->Afunc.definition))
return 1;
if (strcmp(p1->Bfunc.definition, p2->Bfunc.definition))
return 1;
if (strcmp(p1->Cfunc.definition, p2->Cfunc.definition))
return 1;
break;
case SMPAL_COLOR_MODE_GRADIENT: {
int i=0;
if (p1->gradient_num != p2->gradient_num)
return 1;
for (i=0; igradient_num; ++i) {
if (p1->gradient[i].pos != p2->gradient[i].pos)
return 1;
if (p1->gradient[i].col.r != p2->gradient[i].col.r)
return 1;
if (p1->gradient[i].col.g != p2->gradient[i].col.g)
return 1;
if (p1->gradient[i].col.b != p2->gradient[i].col.b)
return 1;
}
break;
case SMPAL_COLOR_MODE_CUBEHELIX:
return 1;
break;
} /* case GRADIENT */
} /* switch() */
return 0; /* no real difference found */
}
#define CONSTRAIN(x) ((x) < 0 ? 0 : ((x) > 1 ? 1 : (x)))
/* This one takes the gradient defined in sm_palette.gradient and
* returns an interpolated color for the given gray value. It
* does not matter whether RGB or HSV or whatever values are stored
* in sm_palette.gradient[i].col, they will simply be interpolated.
* Return 0 on success, 1 if gray outside the range covered by the
* gradient. No gamma correction is done. The user can implement
* gamma correction specifying more points.
* sm_palette.gradient[] should be sorted according to .pos.
* Returns 1 on failure and fills color with "nearest" color.
*/
static int
interpolate_color_from_gray(double gray, rgb_color *color)
{
int idx, maxidx;
rgb_color *col1;
rgb_color *col2;
if (gray < 0) {
color->r = sm_palette.gradient[0].col.r;
color->g = sm_palette.gradient[0].col.g;
color->b = sm_palette.gradient[0].col.b;
return 1;
}
maxidx = sm_palette.gradient_num;
if (gray > 1) {
color->r = sm_palette.gradient[maxidx-1].col.r;
color->g = sm_palette.gradient[maxidx-1].col.g;
color->b = sm_palette.gradient[maxidx-1].col.b;
return 1;
}
/* find index by bisecting */
idx = 0;
if (maxidx > 1) {
int topidx = maxidx - 1;
/* treat idx as though it is bottom index */
while (idx != topidx) {
int tmpidx = (idx + topidx) / 2;
if (sm_palette.gradient[tmpidx].pos < gray)
idx = tmpidx + 1; /* round up */
else
topidx = tmpidx;
}
}
col2 = & sm_palette.gradient[idx].col;
if (gray == sm_palette.gradient[idx].pos) {
/* exact hit */
color->r = col2->r;
color->g = col2->g;
color->b = col2->b;
} else {
/* linear interpolation of two colors */
double dx = sm_palette.gradient[idx].pos
- sm_palette.gradient[idx - 1].pos;
double f = (gray - sm_palette.gradient[idx-1].pos) / dx;
col1 = & sm_palette.gradient[idx - 1].col;
color->r = (col1->r + f * (col2->r - col1->r));
color->g = (col1->g + f * (col2->g - col1->g));
color->b = (col1->b + f * (col2->b - col1->b));
}
return 0;
}
#ifndef GPLT_X11_MODE
/* Fills color with the values calculated from sm_palette.[ABC]func
* The color values are clipped to [0,1] without further notice.
* Returns 0 or does an int_error() when function evaluatin failed.
* The result is not in RGB color space jet.
*/
static int
calculate_color_from_formulae(double gray, rgb_color *color)
{
struct value v;
double a, b, c;
#define NO_CARET (-1)
(void) Gcomplex(&sm_palette.Afunc.dummy_values[0], gray, 0.0);
evaluate_at(sm_palette.Afunc.at, &v);
if (undefined)
int_error(NO_CARET,
"Undefined value first color during function evaluation");
a = real(&v);
a = CONSTRAIN(a);
(void) Gcomplex(&sm_palette.Bfunc.dummy_values[0], gray, 0.0);
evaluate_at(sm_palette.Bfunc.at, &v);
if (undefined)
int_error(NO_CARET,
"Undefined value second color during function evaluation");
b = real(&v);
b = CONSTRAIN(b);
(void) Gcomplex(&sm_palette.Cfunc.dummy_values[0], gray, 0.0);
evaluate_at(sm_palette.Cfunc.at, &v);
if (undefined)
int_error(NO_CARET,
"Undefined value third color during function evaluation");
c = real(&v);
c = CONSTRAIN(c);
color->r = a;
color->g = b;
color->b = c;
#undef NO_CARET
return 0;
}
#endif /* !GPLT_X11_MODE */
/* Map gray in [0,1] to color components according to colorMode */
static void
color_components_from_gray(double gray, rgb_color *color)
{
if (gray < 0)
gray = 0;
else if (gray > 1.0)
gray = 1.0;
switch(sm_palette.colorMode) {
default: /* Can't happen */
case SMPAL_COLOR_MODE_GRAY:
color->r = color->g = color->b = pow(gray, 1.0/sm_palette.gamma);
return; /* all done, no color space transformation needed */
case SMPAL_COLOR_MODE_RGB:
color->r = GetColorValueFromFormula(sm_palette.formulaR, gray);
color->g = GetColorValueFromFormula(sm_palette.formulaG, gray);
color->b = GetColorValueFromFormula(sm_palette.formulaB, gray);
break;
case SMPAL_COLOR_MODE_GRADIENT:
interpolate_color_from_gray(gray, color);
break;
#ifndef GPLT_X11_MODE
case SMPAL_COLOR_MODE_FUNCTIONS:
calculate_color_from_formulae(gray, color);
break;
#endif /* !GPLT_X11_MODE */
case SMPAL_COLOR_MODE_CUBEHELIX: {
double phi, a;
phi = 2. * M_PI * (sm_palette.cubehelix_start/3.
+ gray * sm_palette.cubehelix_cycles);
if (sm_palette.gamma != 1.0)
gray = pow(gray, 1./sm_palette.gamma);
a = sm_palette.cubehelix_saturation * gray * (1.-gray) / 2.;
color->r = gray + a * (-0.14861 * cos(phi) + 1.78277 * sin(phi));
color->g = gray + a * (-0.29227 * cos(phi) - 0.90649 * sin(phi));
color->b = gray + a * ( 1.97294 * cos(phi));
color->r = clip_to_01(color->r);
color->g = clip_to_01(color->g);
color->b = clip_to_01(color->b);
}
break;
}
}
/*
* Map a gray value in [0,1] to the corresponding RGB values in [0,1],
* according to the current colorMode and color space.
*
* Note -- November 2003: this routine has been renamed from color_from_gray()
* to rgb1_from_gray() in order to more clearly distinguish structures
* rgb_color and rgb255_color.
*/
void
rgb1_from_gray(double gray, rgb_color *color)
{
/* get the color */
color_components_from_gray(gray, color);
if (sm_palette.colorMode == SMPAL_COLOR_MODE_GRAY)
return;
/* transform to RGB if necessary */
switch(sm_palette.cmodel) {
default:
case C_MODEL_RGB:
break;
case C_MODEL_HSV:
HSV_2_RGB(color);
break;
case C_MODEL_CMY:
CMY_2_RGB(color);
break;
}
}
/*
* Convenience function to map R, G and B float values [0,1] to uchars [0,255].
*/
void
rgb255_from_rgb1(rgb_color rgb1, rgb255_color *rgb255)
{
rgb255->r = (unsigned char)(255 * rgb1.r + 0.5);
rgb255->g = (unsigned char)(255 * rgb1.g + 0.5);
rgb255->b = (unsigned char)(255 * rgb1.b + 0.5);
}
/*
* Convenience function to map gray values to R, G and B values in [0,1],
* limiting the resolution of sampling a continuous palette to use_maxcolors.
*
* EAM Sep 2010 - Guarantee that for palettes with multiple segments
* (created by 'set palette defined ...') the mapped gray value is always
* in the appropriate segment. This has the effect of overriding
* use_maxcolors if the defined palette has more segments than the nominal
* limit.
*/
void
rgb1maxcolors_from_gray(double gray, rgb_color *color)
{
if (sm_palette.use_maxcolors != 0)
gray = quantize_gray(gray);
rgb1_from_gray(gray, color);
}
double
quantize_gray( double gray )
{
double qgray = gray;
if (sm_palette.gradient_type == SMPAL_GRADIENT_TYPE_DISCRETE)
return qgray;
qgray = floor(gray * sm_palette.use_maxcolors)
/ (sm_palette.use_maxcolors-1);
if (sm_palette.gradient_type == SMPAL_GRADIENT_TYPE_MIXED) {
gradient_struct *g = sm_palette.gradient;
double small_interval = 1. / sm_palette.use_maxcolors;
int j;
/* Backward compatibility with common case of 1 segment */
if ((sm_palette.gradient_num small_interval)
;
/* There is at least one palette segment that is smaller than the sampling
* interval. Earlier versions of quantize_gray() handled this case poorly.
* This version isn't perfect either. In particular it fails to handle the
* converse problematic case where qgray is inside some small interval but
* the true gray value is not. This causes a color from the narrow segment
* to be applied incorrectly to neighboring segments as well.
*/
else for (j=0; j= g[j].pos) && (gray < g[j+1].pos)) {
/* See if it is so small that truncation missed it */
if ((g[j+1].pos - g[j].pos) < small_interval)
qgray = (g[j].pos + g[j+1].pos) / 2.;
break;
}
}
}
if (qgray >= 1.0)
qgray = 1.0;
return qgray;
}
/*
* Convenience function to map gray values to R, G and B values in [0,255],
* taking care of palette maxcolors (i.e., discrete nb of colors).
*/
void
rgb255maxcolors_from_gray(double gray, rgb255_color *rgb255)
{
rgb_color rgb1;
rgb1maxcolors_from_gray(gray, &rgb1);
rgb255_from_rgb1(rgb1, rgb255);
}
/*
* Used by approximate_palette
*/
static double
get_max_dev(rgb_color *colors, int j, double limit)
{
double max_dev = 0.0;
double rdev, gdev, bdev;
double r=colors[0].r, g=colors[0].g, b=colors[0].b;
int i;
double sr = (colors[j].r - r) / j;
double sg = (colors[j].g - g) / j;
double sb = (colors[j].b - b) / j;
for (i=1; i max_dev)
max_dev = rdev;
if(gdev > max_dev)
max_dev = gdev;
if(bdev > max_dev)
max_dev = bdev;
if(max_dev >= limit)
break;
}
return max_dev;
}
/*
* Used by approximate_palette: true if one color component in mid
* is higher (or lower) than both left and right, flase other wise
*/
static int
is_extremum(rgb_color left, rgb_color mid, rgb_color right)
{
/* mid is maximum */
if(left.r < mid.r && mid.r > right.r)
return 1;
if(left.g < mid.g && mid.g > right.g)
return 1;
if(left.b < mid.b && mid.b > right.b)
return 1;
/* mid is minimum */
if(left.r > mid.r && mid.r < right.r)
return 1;
if(left.g > mid.g && mid.g < right.g)
return 1;
if(left.b > mid.b && mid.b < right.b)
return 1;
return 0;
}
#define GROW_GRADIENT(n) do { \
if(cnt == gradient_size) { \
gradient_size += (n); \
gradient = realloc(gradient, gradient_size * sizeof(*gradient));\
} \
} while(0)
/*
* This function takes a palette and constructs a gradient which can
* be used to approximate the palette by linear interpolation.
* The palette is sampled at samples+1 points equally spaced in [0,1].
* From this huge gradient a much smaller one is constructed by selecting
* just those sampling points which still do approximate the full sampled
* one well enough. allowed_deviation determines the maximum deviation
* of all color components which is still acceptable for the reduced
* gradient. Use a sufficiently large number of samples (500 to 5000).
* Please free() the returned gradient after use. samples, allowed_deviation
* and max_skip may be r;
m = col->g;
y = col->b;
col->r = CONSTRAIN(1.0 - c);
col->g = CONSTRAIN(1.0 - m);
col->b = CONSTRAIN(1.0 - y);
}
static void
HSV_2_RGB(rgb_color *col)
{
double h,s,v, f,p,q,t;
int i;
h = col->r;
s = col->g;
v = col->b;
if (s == 0) { /* achromatic (gray) */
col->r = col->g = col->b = v;
return;
}
/* Apply HSV offset */
h += sm_palette.HSV_offset;
if (h > 1.0) h -= 1.0;
h *= 6.; /* h range in gnuplot is [0,1] and not the usual [0,360] */
i = floor(h);
f = h - i;
p = v * (1.0 - s);
q = v * (1.0 - s*f);
t = v * (1.0 - s*(1.0-f));
switch(i % 6) {
case 0:
col->r = v;
col->g = t;
col->b = p;
break;
case 1:
col->r = q;
col->g = v;
col->b = p;
break;
case 2:
col->r = p;
col->g = v;
col->b = t;
break;
case 3:
col->r = p;
col->g = q;
col->b = v;
break;
case 4:
col->r = t;
col->g = p;
col->b = v;
break;
default:
col->r = v;
col->g = p;
col->b = q;
break;
}
}
#undef CONSTRAIN
/*
* Support for user-callable rgb = hsv2rgb(h,s,v)
*/
unsigned int
hsv2rgb ( rgb_color *color )
{
HSV_2_RGB(color);
return ((unsigned int)(255.*color->r) g) b));
}
/*
* Get the index of the gradient segment corresponding to the gray value
* from the discrete gradient palette.
*/
int
index_from_gray (double gray)
{
int idx, maxidx;
if (gray = 1)
return maxidx - 1;
/* find index by bisecting */
idx = 0;
if (maxidx > 1) {
int topidx = maxidx - 1;
/* treat idx as though it is bottom index */
while (idx != topidx) {
int tmpidx = (idx + topidx) / 2;
if (sm_palette.gradient[tmpidx].pos < gray)
idx = tmpidx + 1; /* round up */
else
topidx = tmpidx;
}
}
return idx-1;
}
/* eof getcolor.c */