Newsgroup: it.comp.lang.c++
Titolo: Luminosita' del colore

Ciao,
come si fa a sapere dal valore RGB di un colore se questo e' scuro o
chiaro, in pratica come si fa a ricavare la luminosita' del colore dal
valore RGB.
Grazie Antonio


Ti do questo estratto di una classe che stavo scrivendo... dovrebbe
bastarti.
#ifndef COLOR_CLASS
#define COLOR_CLASS
extern "C" {
#include <math.h>
}
class Color {
    double red, green, blue;
    void hsb_to_rgb(double h, double s, double v, double &r, double &g, 
double &b );
    void rgb_to_hsb(double r, double g, double b, double &h, double &s, 
double &v );
    double value(double n1, double n2, double h );
    void hls_to_rgb(double h, double l, double s, double &r, double &g, 
double &b );
    void rgb_to_hls(double r, double g, double b, double &h, double &l, 
double &s );
    // hsb: hue, saturation, brightness (value)
    // hsl: hue, lightness, saturation
public:
    Color(double r = 0.0, double g = 0.0, double b = 0.0 ) : red(r), 
green(g), blue(b) {}
    Color(const Color &a ) : red(a.red), green(a.green), blue(a.blue) {}
    double Get_Red  (void )                   			{ return red; }
    double Get_Green(void )                       	{ return green; }
    double Get_Blue (void )        						{ return blue; }
    void   Get_Rgb(double &r, double &g, double &b ) { r=red; g=green; 
b=blue; }
    double Get_Cyan   (bool cmyk = false )			{ return (cmyk == true) ?
(max(red,green,blue) - red)   : (1.0 - red); }
    double Get_Magenta(bool cmyk = false )		{ return (cmyk == true) ?
(max(red,green,blue) - green) : (1.0 - green); }
    double Get_Yellow (bool cmyk = false )      	{ return (cmyk == true)
? (max(red,green,blue) - blue)  : (1.0 - blue); }
    double Get_Black  (void )                       		{ return 1.0 -
max(red,green,blue); }
    void  Get_Cmy(double &c, double &m, double &y ) { c=1.0-red;
m=1.0-green; y=1.0-blue; }
    void  Get_Cmyk(double &c, double &m, double &y, double &k
)           
    {
    	const double white = max(red,green,blue);
		c = white - red;
		m = white - green;
 		y = white - blue;
		k = 1.0 - white;
	}
 
    double Get_Hue(void );
    double Get_Saturation(void )                    
    {
 		double v = max(red, green, blue),
                    m =  min(red, green, blue), s;
		if ((v == 0.0) || (v == m)) 	s = 0.0;
		else                        			s = (v - m) / v;
		return s;
}
	double Get_Lightness(void )                     { return (max(red,
green, blue) + min(red, green, blue)) / 2; }
    double Get_Brightness(void )                    { return max(red,
green, blue); }
    void Get_Hsb(double &h, double &s, double &v )  { rgb_to_hsb(red,
green, blue, h, s, v); }
    void Get_Hls(double &h, double &l, double &s )  { rgb_to_hls(red,
green, blue, h, l, s); }
    double Get_Y(void ) { return (red * 0.2989    + green * 0.5866    +
blue * 0.1144);    }
    double Get_U(void ) { return (red * (-0.1473) + green * (-0.2891) +
blue * 0.4364);    }
    double Get_V(void ) { return (red * 0.6149    + green * (-0.5145) +
blue * (-0.1004)); }
    double Get_I(void ) { return (red * 0.5959    + green * (-0.2741) +
blue * (-0.3218)); }
    double Get_Q(void ) { return (red * 0.2113    + green * (-0.5227) +
blue * 0.3113);    }
    void   Get_Yuv(double &y, double &u, double &v ) { y=Get_Y();
u=Get_U(); v=Get_V(); }
    void   Get_Yiq(double &y, double &i, double &q ) { y=Get_Y();
i=Get_I(); q=Get_Q(); }
    void Set_Rgb (double r, double g, double b )            { red=r;    
green=g;     blue=b; }
    void Set_Cmy (double c, double m, double y )            { red=1.0-c;
green=1.0-m; blue=1.0-y; }
    void Set_Cmyk(double c, double m, double y, double k )  { c+=k;
m+=k; y+=k; Set_Cmy(c,m,y); }
    void Set_Hsb (double h, double s, double v )            {
hsb_to_rgb(h,s,v,red,green,blue); }
    void Set_Hls (double h, double l, double s )            {
hsb_to_rgb(h,l,s,red,green,blue); }
    void Set_Yuv (double y, double u, double v )            { red  =
y*1.000100 + u*0.000141 + v*1.140171;
 
green= y*1.000100 - u*0.395830 - v*0.580966;
 
blue = y*1.000100 + u*2.029299 - v*0.000023; }
    void Set_Yiq (double y, double i, double q )            { red  =
y*1.000162 + i*0.956268 + q*0.620972;
 
green= y*1.000035 - i*0.271705 - q*0.648374;
 
blue = y*1.000270 - i*1.105300 + q*1.702164; }
    void operator = (Color &c )
    {
        red = c.red;
        green = c.green;
        blue = c.blue;
    }
    bool operator != (Color &c )
    {
        return ((red != c.red) || (green != c.green) || (blue !=
c.blue));
    }
};
#endif /* COLOR_CLASS */
Ciao!
Andrea
--
Posted from 
verde.ph.unito.it
 [193.205.64.100]
via Mailgate.ORG Server - 
http://www.mailgate.org