0

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

VMMR
  • 13
  • 1
  • 1
    But we can't help you with no code at all! What is your function? And what is that "unique function on the internet" in javascript? (not a link, the function!) – chrslg Jan 04 '23 at 00:41
  • @chrslg I tried to send my code, but the site wasn't letting me :I – VMMR Jan 04 '23 at 01:43

1 Answers1

1

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")
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Thank you very much for your help. I'm kind of new to VB.NET, I admit it's kind of embarrassing not being able to convert something that most people find so simple haha :) Anyway, thanks for the help, and your name in credits of my program is a certainty. – VMMR Jan 04 '23 at 03:16