gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > #FF00FF to gba color.

#47960 - QuantumDoja - Fri Jul 15, 2005 11:45 am

Hi, I have "Fuchsia" a purpleish color that is:
#FF00FF in web colors
255 - red
0 - green
255 - blue
16711935 as a microsoft access variable

I want to convert to a gba color, but end up with: 0x2108, and in the palette it is a dark grey, here is my code:

its vb.net

Code:


Public Function GetGBAColor(ByVal strWebSafe) As Integer
        Dim r, g, b
        Dim i As Integer

        r = Mid(strWebSafe, 1, 2)
        g = Mid(strWebSafe, 3, 2)
        b = Mid(strWebSafe, 5, 2)
        Dim rc As Integer
        Dim gc As Integer
        Dim bc As Integer

        For i = 0 To 255
            If Hex$(i) = r Then
                rc = i
            End If
        Next
        For i = 0 To 255
            If Hex$(i) = g Then
                gc = i
            End If
        Next
        For i = 0 To 255
            If Hex$(i) = b Then
                bc = i
            End If
        Next

        rc = rc / 11 + 8
        gc = gc / 11 + 8
        bc = bc / 11 + 8

        GetGBAColor = rc + (32 * gc) + (1024 * bc)
    End Function

_________________
Chris Davis

#47961 - QuantumDoja - Fri Jul 15, 2005 11:56 am

I just walked through my code and notieced it want returning the integer value from a hex code, probably why its not working...sorry.
_________________
Chris Davis

#47963 - QuantumDoja - Fri Jul 15, 2005 12:02 pm

Code:

Public Function GetGBAColor(ByVal strWebSafe) As Integer
        Dim r, g, b
        Dim i As Integer

        r = Mid(strWebSafe, 1, 2)
        g = Mid(strWebSafe, 3, 2)
        b = Mid(strWebSafe, 5, 2)
        Dim rc As Integer
        Dim gc As Integer
        Dim bc As Integer

        For i = 0 To 255
            If UCase(Hex$(i).ToString) = UCase(r) Then
                rc = i
            End If
        Next
        For i = 0 To 255
            If UCase(Hex$(i).ToString) = UCase(g) Then
                gc = i
            End If
        Next
        For i = 0 To 255
            If UCase(Hex$(i).ToString) = UCase(b) Then
                bc = i
            End If
        Next

        rc = rc / 11 + 8
        gc = gc / 11 + 8
        bc = bc / 11 + 8

        GetGBAColor = rc + (32 * gc) + (1024 * bc)
    End Function

_________________
Chris Davis

#47964 - NoMis - Fri Jul 15, 2005 12:04 pm

The GBA uses a different color format as your PC. A color is made up of a 16bit integer that has 5 bits each color plus an unused bit. So every color has a range of 0 to 31 giving you 32,768 as the maximal number of colors

Red begins at the lowest order bit, followed by green than comes blue.

The following macro simplifies the use of colors.
Code:
#define RGB16(r,g,b)  (((b)<<10)+((g)<<5)+(r))


Your color is composed by blue and red colors and with the use of that macro it would look like this.
Code:
int colorvalue = RGB16(31,0,31);


EDIT:

With that knowlege you can easily recalculate the range of colors.
A color Value of 255 would be 31 on the GBA so you would have to calculate like this.

Code:
ColorRGBGBA = 31/255 * ColorRGBPC


NoMis
_________________
www.gamedev.at - The austrian gamedev site
hde.gamedev.at - The Handheld Dev Env plugins for Eclipse

#47965 - Mighty Max - Fri Jul 15, 2005 12:13 pm

Hey there,

I just noticed your way to receive the int values from the hex string. That is a very slow method to, you might want to change it a bit.

I don't know the keywords for basic (ill use replacements - its too long ago ;) ), but you might change it a bit like

Code:

value = 0
for charpos = 1 to <maxhexchars> do
  if ( CharAt(charpos)<"A") then
    value = value * 16 + ASCIICODE(CharAt(charpos)) - ASCIICODE("0")
  else
    value = value * 16 + ASCIICODE(CharAt(charpos)) - ASCIICODE("A") + 10
  endif
next


That removes unneccesary loops.

#47966 - tepples - Fri Jul 15, 2005 1:07 pm

NoMis wrote:
With that knowlege you can easily recalculate the range of colors.
A color Value of 255 would be 31 on the GBA so you would have to calculate like this.

Code:
ColorRGBGBA = 31/255 * ColorRGBPC

That's true on the Game Boy Player and Nintendo DS, but the GBA and GBA SP displays are so dark that anything from 0-7 will appear black in most lighting situations. The first Castlevania game for GBA didn't take this into account, which is why it looks so dark on anything but a Game Boy Player or a Nintendo DS. I'm about 99 percent sure that the "rc = rc / 11 + 8" is an attempt to compensate for this.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.