1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Antoine Cellerier <dionoea -at- videolan -dot- org>
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
22 /***********************************************************************
23 * Colorspace transformations
24 ***********************************************************************
25 * r, g and b range from 0 to 255
26 * h ranges from 0 to 3599 (which in fact means 0.0 to 359.9).
27 * 360 is the same as 0 (it loops)
28 * s and v range from 0 to 255 (which in fact means 0.00 to 1.00)
29 ***********************************************************************/
31 void rgb2hsv( int r
, int g
, int b
, int *h
, int *s
, int *v
)
37 if( b
> max
) max
= b
;
40 if( b
< min
) min
= b
;
45 *h
= 0; *s
= 0; /* Random since it's black */
50 *h
= 0; /* Random since it's gray */
52 else if( max
== r
&& g
>= b
)
54 *h
= ( 10 * 60 * ( g
- b
)/( max
- min
) );
56 else if( max
== r
&& g
< b
)
58 *h
= ( 10 * ( 60 * ( g
- b
)/( max
- min
) + 360 ));
62 *h
= ( 10 * ( 60 * ( b
- r
)/( max
- min
) + 120 ));
66 *h
= ( 10 * ( 60 * ( r
- g
)/( max
- min
) + 240 ));
70 while( *h
< 0 ) *h
+= 3600;
71 while( *h
>= 3600 ) *h
-= 3600;
73 *s
= (( max
- min
)*255)/max
;
77 void hsv2rgb( int h
, int s
, int v
, int *r
, int *g
, int *b
)
80 while( h
< 0 ) h
+= 3600;
81 while( h
>= 3600 ) h
-= 3600;
83 p
= ( v
* ( 255 - s
) ) / ( 255 );
84 q
= ( v
* ( 600*255 - f
*s
) ) / ( 255 * 600 );
85 t
= ( v
* ( 600*255 - ( 600 - f
) * s
) ) / ( 255 * 600 );
88 if( s
== 0 ) /* gray */