You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

135 lines
3.1 KiB

// From grayscale
struct gmt_color Gray2RGB() const
{
struct gmt_color color;
color.transparency=transparency;
color.model=RGB;
color.r=color.g=color.b=gray;
return color;
}
struct gmt_color Gray2HSV() const
{
struct gmt_color color;
color.transparency=transparency;
color.model=HSV;
color.hue=0;
color.saturation=0;
color.value=gray/255.0;
return color;
}
struct gmt_color Gray2CMYK() const
{
struct gmt_color color;
color.transparency=transparency;
color.model=CMYK;
color.cyan=color.magenta=color.yellow=0;
color.black=(1.0-gray/255.0)*100.0;
return color;
}
// From RGB
struct gmt_color RGB2Gray() const
{
struct gmt_color color;
color.transparency=transparency;
color.model=GRAY;
color.gray=0.2126*r+0.7152*g+0.0722*b;
return color;
}
struct gmt_color RGB2HSV() const
{
struct gmt_color color;
color.transparency=transparency;
color.model=HSV;
double rr=r/255.0,gg=g/255.0,bb=b/255.0;
double cmax=std::max(rr,std::max(gg,bb));
double cmin=std::min(rr,std::min(gg,bb));
double delta=cmax-cmin;
if(0==delta) color.hue=0;
else if(cmax==rr)
{
double x=(gg-bb)/delta;
color.hue=x-floor(x/6.0)*6.0;
}
else if(cmax==gg) color.hue=(bb-rr)/delta+2.0;
else if(cmax==bb) color.hue=(rr-gg)/delta+4.0;
color.hue*=60.0;
color.saturation=(0==cmax)?0:(delta/cmax);
color.value=cmax;
return color;
}
struct gmt_color RGB2CMYK() const
{
struct gmt_color color;
color.transparency=transparency;
color.model=CMYK;
double rr=r/255.0,gg=g/255.0,bb=b/255.0;
double k=1.0-std::max(rr,std::max(gg,bb));
color.black=k;
color.cyan=(1.0-rr-k)/(1.0-k);
color.magenta=(1.0-gg-k)/(1.0-k);
color.yellow=(1.0-bb-k)/(1.0-k);
if(0.0==1.0-k) color.cyan=color.magenta=color.yellow=0.0;
color.black*=100.0;
color.cyan*=100.0;
color.magenta*=100.0;
color.yellow*=100.0;
return color;
}
// From HSV
struct gmt_color HSV2Gray() const
{
return HSV2RGB().RGB2Gray();
}
struct gmt_color HSV2RGB() const
{
struct gmt_color color;
color.transparency=transparency;
color.model=RGB;
double c=saturation*value;
double x=c*(1.0-fabs((hue/60.0)-floor((hue/60.0)/2.0)*2.0-1.0));
double m=value-c;
if(0.0 <=hue && 60.0 >hue) {color.r=c; color.g=x; color.b=0;}
if(60.0 <=hue && 120.0>hue) {color.r=x; color.g=c; color.b=0;}
if(120.0<=hue && 180.0>hue) {color.r=0; color.g=c; color.b=x;}
if(180.0<=hue && 240.0>hue) {color.r=0; color.g=x; color.b=c;}
if(240.0<=hue && 300.0>hue) {color.r=x; color.g=0; color.b=c;}
if(300.0<=hue &&360.0>=hue) {color.r=c; color.g=0; color.b=x;}
color.r+=m; color.g+=m; color.b+=m;
color.r*=255; color.g*=255; color.b*=255;
return color;
}
struct gmt_color HSV2CMYK() const
{
return HSV2RGB().RGB2CMYK();
}
// From CMYK
struct gmt_color CMYK2Gray() const
{
return CMYK2RGB().RGB2Gray();
}
struct gmt_color CMYK2RGB() const
{
struct gmt_color color;
color.transparency=transparency;
color.model=RGB;
double cc=cyan/100.0,mm=magenta/100.0,yy=yellow/100.0,kk=black/100.0;
color.r=255.0*(1.0-cc)*(1.0-kk);
color.g=255.0*(1.0-mm)*(1.0-kk);
color.b=255.0*(1.0-yy)*(1.0-kk);
return color;
}
struct gmt_color CMYK2HSV() const
{
return CMYK2RGB().RGB2HSV();
}