RGB nach HSV und HSV nach RGB

A. R. Smith entwickelte das Hue/Saturation/Value-Model 1978. Es basiert auf intuitiven Farbcharakteristika wie Farben, Reinheit und Intensität.

HSV.jpg

Das Koordinatensystem ist ein Zylinder, in dem die Farben zum Sechseck angeordnet sind. Der Wert für Hue, den Farbton, läuft von 0 bis 360°. Die Sättigung ist ein Maß für die Reinheit und liegt zwischen 0 und 1, wobei 1 für die reine Farbe steht und durch das Beimischen von Weiß nimmt die Reinheit abnimmt. Die Helligkeit liegt zwischen 0 und 1, wobei 0 für Schwarz steht.

Für die Transformation von RGB nach HSV gibt es keine Matrix, aber einen Algorithmus:

// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
//		if s == 0, then h = -1 (undefined)
void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v )
{
	float min, max, delta;
	min = MIN( r, g, b );
	max = MAX( r, g, b );
	*v = max;				// v
	delta = max - min;
	if( max != 0 )
		*s = delta / max;		// s
	else {
		// r = g = b = 0		// s = 0, v is undefined
		*s = 0;
		*h = -1;
		return;
	}
	if( r == max )
		*h = ( g - b ) / delta;		// between yellow & magenta
	else if( g == max )
		*h = 2 + ( b - r ) / delta;	// between cyan & yellow
	else
		*h = 4 + ( r - g ) / delta;	// between magenta & cyan
	*h *= 60;				// degrees
	if( *h < 0 )
		*h += 360;
}
void HSVtoRGB( float *r, float *g, float *b, float h, float s, float v )
{
	int i;
	float f, p, q, t;
	if( s == 0 ) {
		// achromatic (grey)
		*r = *g = *b = v;
		return;
	}
	h /= 60;			// sector 0 to 5
	i = floor( h );
	f = h - i;			// factorial part of h
	p = v * ( 1 - s );
	q = v * ( 1 - s * f );
	t = v * ( 1 - s * ( 1 - f ) );
	switch( i ) {
		case 0:
			*r = v;
			*g = t;
			*b = p;
			break;
		case 1:
			*r = q;
			*g = v;
			*b = p;
			break;
		case 2
			*r = p;
			*g = v;
			*b = t;
			break;
		case 3:
			*r = p;
			*g = q;
			*b = v;
			break;
		case 4:
			*r = t;
			*g = p;
			*b = v;
			break;
		default:		// case 5:
			*r = v;
			*g = p;
			*b = q;
			break;
	}
}

RGB nach ITU Rec 709 (International Televisision Union – HDTV)

  R709        3.2404 –1.5371 –0.4985       X
[ G709 ] = [ -0.9692  1.8759  0.0415 ] • [ Y ]
  B709        0.0556 –0.2040  1.0573       Z
  X        0.4124  0.3575  0.1804       R709
[ Y ] = [  0.2126  0.7151  0.0721 ] • [ G709 ]
  Z        0.0193  0.1191  0.9502       B709

RGB Rec 709 nach RGB SMPTE 240M

  R709        0.9395  0.0501  0.0102       R240M
[ G709 ] = [  0.0177  0.9657  0.0164 ] • [ R240M ]
  B709       -0.0016  0.0043  1.0059       R240M

EBU 3213 nach Rec. 709

  R709        1.0440 –0.0440  0.0          REBU
[ G709 ] = [  0.0     1.0     0.0    ] • [ REBU ]
  B709        0.0     0.0117  0.9882       REBU