Login Register
Frontpage Code library Pastebin

HSL <-> RGB muunnos

Author: KilledWhale
Added: 10.4.2011 9:52
Edited: 15.4.2011 22:19
Category: Grafiikka

Description

Funktiot HSL (Hue Saturation Light) värien muuntamiseen RGB (Red Green Blue) väriksi ja päin vastoin.

Code

Select all
  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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
For x = 0 To 360
	For y = 0 To 255
		Color HSLtoRGB(x, 255, y, 1), HSLtoRGB(x, 255, y, 2), HSLtoRGB(x, 255, y, 3)
		Dot x, y
		DrawScreen OFF
	Next y
Next x

DrawScreen
WaitKey

// Muuntaa HSL muodossa olevan värin RGB muotoon
// h# = väri (0 - 360)
// s# = kylläisyys (0 - 255)
// l# = valo (0 - 255)
// comp = palautettava komponentti (1 = R, 2 = G, 3 = B)
Function HSLtoRGB(h#, s#, l#, comp)
	s# = s# / 255
	l# = l# / 255
	h# = h# / 360
	
	If l# < 0.5 Then
		q# = l * (1.0 + s)
	Else
		q# = l + s - (l * s)
	EndIf
	
	p# = 2.0 * l - q
	
	tr# = h + 1.0 / 3.0
	tg# = h
	tb# = h - 1.0 / 3.0
	
	If tr < 0 Then
		tr = tr + 1
	ElseIf tr > 1 Then
		tr = tr - 1
	EndIf
	
	If tg < 0 Then
		tg = tg + 1
	ElseIf tg > 1 Then
		tg = tg - 1
	EndIf
	
	If tb < 0 Then
		tb = tb + 1
	ElseIf tb > 1 Then
		tb = tb - 1
	EndIf
	
	If tr# < 1.0 / 6.0 Then
		r# = p + ((q - p) * 6 * tr)
	ElseIf tr# < 1.0 / 2.0 Then
		r# = q
	ElseIf tr# < 2.0 / 3.0 Then
		r# = p + ((q - p) * 6 * (2.0 / 3.0 - tr)) 
	Else
		r# = p
	EndIf
	
	If tg# < 1.0 / 6.0 Then
		g# = p + ((q - p) * 6 * tg)
	ElseIf tg# < 1.0 / 2.0 Then
		g# = q
	ElseIf tg# < 2.0 / 3.0 Then
		g# = p + ((q - p) * 6 * (2.0 / 3.0 - tg)) 
	Else
		g# = p
	EndIf
	
	If tb# < 1.0 / 6.0 Then
		b# = p + ((q - p) * 6 * tb)
	ElseIf tb# < 1.0 / 2.0 Then
		b# = q
	ElseIf tb# < 2.0 / 3.0 Then
		b# = p + ((q - p) * 6 * (2.0 / 3.0 - tb)) 
	Else
		b# = p
	EndIf
	
	Select comp
		Case 1
			Return r * 255
		Case 2
			Return g * 255
		Case 3
			Return b * 255
	EndSelect
EndFunction

// Muuntaa RGB muodossa olevan värin HSL muotoon
// r# = punainen (0 - 255)
// g# = virhreä (0 - 255)
// b# = sininen (0 - 255)
// comp = palautettava komponentti (1 = H, 2 = S, 3 = L)
Function RGBtoHSL(r#, g#, b#, comp)
	r# = r / 255
	g# = g / 255
	b# = b / 255
	
	ma# = Max(Max(r, g), b)
	mi# = Min(Min(r, g), b)
	
	If ma = mi Then
		h# = 0
	ElseIf ma = r then
		h# = 60 * (0 + ((g - b) / (ma - mi)))
	ElseIf ma = g Then
		h# = 60 * (2 + ((b - r) / (ma - mi)))
	ElseIf ma = b Then
		h# = 60 * (4 + ((r - g) / (ma - mi)))
	EndIf
	
	If h# < 0 Then
		h# = h# + 360
	EndIf
	
	l# = 0.5 * (ma + mi)
	
	If ma = mi Then
		s# = 0.0
	ElseIf l <= 0.5 Then
		s# = (ma - mi) / (2 * l)
	ElseIf l > 0.5 Then
		s# = (ma - mi) / (2 - (2 * l))
	EndIf
	
	Select comp
		Case 1
			Return h
		Case 2
			Return s * 255
		Case 3
			Return l * 255
	EndSelect
EndFunction

Comments

No comments. You can be first!

Leave a comment

You must be logged in to comment.