Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / DWORD to RGB conversion?

Author
Message
Mr909
12
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 11th Oct 2012 22:14
Is there any way to get the RGB values of a color once it's stored as a DWORD?

For example, I have col_red constant as RGB(255,0,0). If I want to manipulate that red for some reason, say, blend colors, make it lighter, I need to get the individual RGB values to tweak them. But since the RGB(255,0,0) is stored as DWORD, it comes out as some long negative number. I don't know how to turn it back.

Also, can someone link me to a DBPro formatted RGB to HSV algorithm? Much appreciated. I learned the conversion, but I don't want to put in the effort to translate it to DBPro if it's already been done.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 12th Oct 2012 02:04
You need to look at masking and hexadecimal numbers. The DBP colours are very easy to understand once you see how they work. For example:

myColor = 0xFFFF0000

Would be red. Why? If we take a closer look at that number, we see that we can split it up into an understandable form:



The function rgb() is the "nooby" way of handling colours. The above is exactly the same as this:



In answer to your question, there are 2 ways of getting the individual components. There's the nooby way, and there's the bit masking way.

The nooby way



Bit masking

You mentioned that you used constants at the beginning of your program. I'm assuming you did something like this:



And now you want to get all of those together into one variable, right? All we have to do is use bitwise operators:



MyColor should now have the value 0xFFAABBCC. Note that you can do this in any order you want.

Getting the colours back out of myColor would look like this:



You might also want to shift the bits so they do the exact same thing as the rgbr(), rgbg() and rgbb() commands:



TheComet

"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
Mr909
12
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 12th Oct 2012 03:06
Thanks. As to the "Noob" technique, it's the one I'm going with for a reason, simple to understand and it'll make sense to players trying to input color values (Although it'll be scaled back). Most normal people don't see color in hex values, call us whatever you like. Also, converting hex to HSV sounds like it'll be another deal entirely, where as I already have notes on converting RGB to HSV.

Thank you for your help.

Login to post a reply

Server time is: 2024-11-27 10:10:15
Your offset time is: 2024-11-27 10:10:15