tri-hex'es

Blitz3D Forums/Blitz3D Programming/tri-hex'es

chwaga(Posted 2007) [#1]
is there any function to convert an rgb to a tri-hex?
for example:
Function Tri-Hex-It(red, green, blue)

;????

End Function



_33(Posted 2007) [#2]
What's a Tri-Hex?


chwaga(Posted 2007) [#3]
Non-standard word?? *shrug*

A tri-hex is a hexadecimal RGB example: $ffffff is white

They're also called hexadecimal triplets
hexadecimal info


_33(Posted 2007) [#4]
AH!

Function GetRed(rgb)
   Return rgb Shr 16 And %11111111 
End Function 

Function GetGreen(rgb)
   Return rgb Shr 8 And %11111111 
End Function 

Function GetBlue(rgb) 
   Return rgb And %11111111 
End Function

Function GetRGB(red,green,blue)
   Return blue Or (green Shl 8) Or (red Shl 16)
End Function



chwaga(Posted 2007) [#5]
excuse me for being a total idiot, but how do I use those?


_33(Posted 2007) [#6]
tri_hex_thingy% = GetRGB(10, 20, 30)

or any value. More hints?


chwaga(Posted 2007) [#7]
thanks


Gillissie(Posted 2007) [#8]
I think this is what he's looking for:

; Ascii value for each hex digit
Dim giHexDigit(15)
giHexDigit(0) = 48
giHexDigit(1) = 49
giHexDigit(2) = 50
giHexDigit(3) = 51
giHexDigit(4) = 52
giHexDigit(5) = 53
giHexDigit(6) = 54
giHexDigit(7) = 55
giHexDigit(8) = 56
giHexDigit(9) = 57
giHexDigit(10) = 65
giHexDigit(11) = 66
giHexDigit(12) = 67
giHexDigit(13) = 68
giHexDigit(14) = 69
giHexDigit(15) = 70


Function RGB_To_TriHex$(iR%,iG%,iB%)
	Return ToHex8(iR) + ToHex8(iG) + ToHex8(iB)
End Function


Function FromHex8%(sHex$)
	Local iValue%
	If Len(sHex) <> 2 Then Return 0

	sHex = Upper(sHex)
	
	For i = 0 To 15
		If giHexDigit(i) = Asc(Right(sHex,1)) Then
			iValue = i
			Exit
		End If
	Next

	For i = 0 To 15
		If giHexDigit(i) = Asc(Left(sHex,1)) Then
			iValue = iValue + i * 16
			Exit
		End If
	Next
	
	Return iValue
End Function


Function ToHex8$(iValue%)
	; iValue must be 0-255 (8-bits)
	If iValue > 255 Or iValue < 0 Then RuntimeError("Invalid value passed to ToHex8() function.")
	Local iLow% = iValue Mod 16
	Local iHi% = iValue Shr 4
	
	Return Chr(giHexDigit(iHi)) + Chr(giHexDigit(iLow))
End Function


Function Set_Color_Hex(sColorHex$)
	Color FromHex8(Left(sColorHex,2)),FromHex8(Mid(sColorHex,3,2)),FromHex8(Right(sColorHex,2))
End Function


Example of using it:
Include "Hex8.bb"

Graphics 640,480
SetBuffer BackBuffer()

; Convert RGB values to "Tri-Hex" string.
Text 0,0,RGB_To_TriHex(255,200,100)

; Use a "Tri-Hex" string to set the current color.
Set_Color_Hex("FFAA00")


Oval 200,200,100,100,True

Flip

WaitKey

End



chwaga(Posted 2007) [#9]
thanks guys