Problems creating a BGR555 color converter (VB.NET)

105 views Asked by At

I've been trying to create a function for VB.NET, capable of converting Hexadecimal Color to BGR555 format. As far as I've looked, there's only one function that does this on the internet, but it's done in JavaScript (more details: https://orangeglo.github.io/BGR555/)

I even had help from some people in creating the function, but it still didn't turn out as expected. If anyone finds out why the result is different, I would be very grateful.

Practical example: The original function in JavaScript converted the value #FFFFFF to 7FFF (Big Endian). With the function I created together with others, it returns 2048. https://i.stack.imgur.com/R8hSs.png

1

There are 1 answers

1
Jimi On BEST ANSWER

Not sure why your conversion function converts White (RGB (255, 255, 255)) or #FFFFFF (RGB) to 2048 in BGR555.

Assuming 2048 is hexadecimal, the bits representation is 10000001001000
If it's decimal, the representation is 100000000000
I don't know how you got there, it should be 111111111111111.

BGR555 is defined as

a sRGB format with 16 bits per pixel (BPP).
Each color channel (blue, green, and red) is allocated 5 bits per pixel (BPP).

In VB.NET it could be something like this:

Imports System.Drawing

' Get the Color value from its HTML representation
Dim rgb = ColorTranslator.FromHtml("#FFFFFF")

' Divide R, G and B values by 8 and shifts left by 0, 5 and 10 positions 
' (16 bit pixel, each color in 5 bits)
Dim colorBGR555 = CShort((rgb.R \ 8) + ((rgb.G \ 8) << 5) + ((rgb.B \ 8) << 10))
' Converts to string in Hex Format (Big Endian)  
Dim colorBGR555HexBE = colorBGR555.ToString("X2")

' Reverse the bytes (it's a Short value, two bytes) for its Little Endian representation
Dim bytes = BitConverter.GetBytes(colorBGR555).Reverse().ToArray()
Dim colorBGR555HexLE = BitConverter.ToInt16(bytes, 0).ToString("X2")