File: extensions.cpp

package info (click to toggle)
qonk 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,076 kB
  • ctags: 2,026
  • sloc: cpp: 14,978; sh: 3,464; makefile: 200
file content (85 lines) | stat: -rw-r--r-- 1,199 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2005 by Anthony Liekens anthony@liekens.net
#include <SDL/SDL_gfxPrimitives.h>

#include "extensions.h"

double
frand() {
	return (double)rand() / RAND_MAX;
}

double
frand( double max ) {
	return max * frand();
}

Uint32 HSVtoRGB( float h, float s, float v )
{
	int i;
	float f, p, q, t, r, g, b;

	if( s == 0 ) {

		// achromatic (grey)
		r = g = b = v;

	} else {

		h /= 60;			// sector 0 to 5
		i = (int)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;
		}

	}

	return 256 * 256 * (int)( 255 * r ) + 256 * (int)( 255 * g ) + (int)( 255 * b );

}

Uint8 getRed( Uint32 color ) {
	return ( color & 0xFF0000 ) >> 16;
}

Uint8 getGreen( Uint32 color ) {
	return ( color & 0x00FF00 ) >> 8;
}

Uint8 getBlue( Uint32 color ) {
	return color & 0x0000FF;
}