A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 131 lines 3.4 kB view raw
1/*************************************************************************** 2 * __________ __ ___. 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 * \/ \/ \/ \/ \/ 8 * $Id$ 9 * 10 * Copyright (C) 2006 Antoine Cellerier <dionoea -at- videolan -dot- org> 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * as published by the Free Software Foundation; either version 2 15 * of the License, or (at your option) any later version. 16 * 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 18 * KIND, either express or implied. 19 * 20 ****************************************************************************/ 21 22#include "rgb_hsv.h" 23 24/*********************************************************************** 25 * Colorspace transformations 26 *********************************************************************** 27 * r, g and b range from 0 to 255 28 * h ranges from 0 to 3599 (which in fact means 0.0 to 359.9). 29 * 360 is the same as 0 (it loops) 30 * s and v range from 0 to 255 (which in fact means 0.00 to 1.00) 31 ***********************************************************************/ 32 33void rgb2hsv( int r, int g, int b, int *h, int *s, int *v ) 34{ 35 int max; 36 int min; 37 38 max = r > g ? r : g; 39 if( b > max ) max = b; 40 41 min = r < g ? r : g; 42 if( b < min ) min = b; 43 44 if( max == 0 ) 45 { 46 *v = 0; 47 *h = 0; *s = 0; /* Random since it's black */ 48 return; 49 } 50 else if( max == min ) 51 { 52 *h = 0; /* Random since it's gray */ 53 } 54 else if( max == r && g >= b ) 55 { 56 *h = ( 10 * 60 * ( g - b )/( max - min ) ); 57 } 58 else if( max == r && g < b ) 59 { 60 *h = ( 10 * ( 60 * ( g - b )/( max - min ) + 360 )); 61 } 62 else if( max == g ) 63 { 64 *h = ( 10 * ( 60 * ( b - r )/( max - min ) + 120 )); 65 } 66 else// if( max == b ) 67 { 68 *h = ( 10 * ( 60 * ( r - g )/( max - min ) + 240 )); 69 } 70 71 /* Just in case ? */ 72 while( *h < 0 ) *h += 3600; 73 while( *h >= 3600 ) *h-= 3600; 74 75 *s = (( max - min )*255)/max; 76 *v = max; 77} 78 79void hsv2rgb( int h, int s, int v, int *r, int *g, int *b ) 80{ 81 int f, p, q, t; 82 while( h < 0 ) h += 3600; 83 while( h >= 3600 ) h-= 3600; 84 f = h%600; 85 p = ( v * ( 255 - s ) ) / ( 255 ); 86 q = ( v * ( 600*255 - f*s ) ) / ( 255 * 600 ); 87 t = ( v * ( 600*255 - ( 600 - f ) * s ) ) / ( 255 * 600 ); 88 89 90 if( s == 0 ) /* gray */ 91 { 92 *r = v; 93 *g = *r; 94 *b = *r; 95 return; 96 } 97 98 switch( h/600 ) 99 { 100 case 0: 101 *r = v; 102 *g = t; 103 *b = p; 104 break; 105 case 1: 106 *r = q; 107 *g = v; 108 *b = p; 109 break; 110 case 2: 111 *r = p; 112 *g = v; 113 *b = t; 114 break; 115 case 3: 116 *r = p; 117 *g = q; 118 *b = v; 119 break; 120 case 4: 121 *r = t; 122 *g = p; 123 *b = v; 124 break; 125 case 5: 126 *r = v; 127 *g = p; 128 *b = q; 129 break; 130 } 131}