SRL Archive - OSR Advanced Scripting Tutorials

Using RGB & Hexadecimal


pwnaz0r

2007-11-21 19:40:30

RGB & Hexademical Tutorial


Introduction

This tutorial will be mostly centered on those who script in Delphi. I will explain the two color spaces RGB(red, blue, green) and Hexadecimal (base-16 or jus Hex), and will explain the relationship between the two, advantages for using each, and making procedures with them.

RGB Explaination


Introduction to RGB
Ah yes, RGB. RGB stands for Red, Blue, Green as stated before. If you do not know the relationship between these, they are the three primary colors, which means that every color ever invented was a makeup of these three colors. In an RGB color value, you have the values.
  • Red
  • Blue
  • Green
All values can have a value from 0 - 255, or 256 true colors. Simple enough eh?

If we want to get literal about it, there is also a fourth value to RGB, and that is Alpha. Alpha is the transparency of the color.

RGB is the most used Colorspace, as it is one of the easiest to display, since it uses the three primary colors.

Why Black is 0 and White is 16777215

Ok, here is the simple reason to why these are the colors they are. White is the presence of light, black is the absence of right. The max value for Color32 is 16777215(white) because there are three values in RGB (red, blue, green) and 256 true colors could be for each value. So the equation for the max color would of course be

256³

Since Black is the absence of light, its RGB value is (0, 0, 0). Since White is the presence of absolute light, its values are (255, 255, 255).

24 Bit RGB

The average RGB value uses 3- 8 bit (one regular byte) values (one byte per R, G, B value). Therefore, it is the gives the truest, most accurate rendition of the color.

Here are some color conversions using 24 - Bit RGB.

(0, 0, 0) is black
(255, 255, 255) is white
(255, 0, 0) is red
(0, 255, 0) is green
(0, 0, 255) is blue
(255, 255, 0) is yellow
(0, 255, 255) is cyan
(255, 0, 255) is magenta


Understanding Memory Space (somewhat useless)

Converting Memory space used by these colors could be somewhat benefitial to your understanding of the importance to clear these values.

Let's say you are using normal 24 bit RGB. Your Screen size is 640 × 480 px.


Memory in bits = 24 * 640 * 480;
Memory in bytes = Memory in bits div 8 //Eight bits in one byte.


Hexadecimal Explaination


Introduction to Hexadecimal

Now, onto the core of this tutorial. Hexadecimal is one of the easiest color spaces to understand, and it is the most use in Bitwise operations(if you don't understand these, find a tutorial on them). Hexidecimal is a 6 character representation for a color using values between 0-9 and then A-F. Here is the main point of converting RGB from hex.


Hex Triplets

Hexidecimal can be split into three 2 character codes called Hex Triplets. We will take, for instance white. Each of these two character codes represent ethier an R, G, or B value.


$FFFFFF

would be split into.


| FF | FF | FF |

Red Green Blue
Value Value Value


Now, here is the equation for finding the R, G, or B value from a Hex Triplet. Rember this table, as each character represents this.


0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
A = 10
B = 11
C = 12
D = 13
E = 14
F = 15

now here is the equation


{first character} * 16 + {second char} = Value


So here is our equation for the values of all three of these


15 * 16 + 15 = 255

You do that for all three of these triplets, since they are all FF. You have all three RGB values at 255 then. So your RGB value = (255, 255, 255) or white :).


Advantages of RGB

RGB uses an easy to understand system of colors. It is easy to work with and edit and it is easy to use in procedures. This is how most of your autocolor procedures where made with.

Advantages of Hexadecimal

Short and sweet, Bitwise operations. They are easy to shift and understand these bits. You could technically make a better autocolor procedure by using bitwise operations, however no one has done yet (it could be you?)

Do you see the relation ship? Hopefully you do.

Procedures using RGB

Making procedures using RGB is simple. Make sure to make use of the ColorToRGB function, already allocated in SCAR. To make your own autocolor procedure, you can go through worlds, collecting color information, then simply find the max and min RGB values.



Procedures using Hexadecimal


function SimilarColors(Color1, Color2, Tolerance: Integer): Boolean;
begin
Result := ((Abs((Color1 and $ff) - (Color2 and $ff)) <= Tolerance) and
(Abs(((Color1 and $ff00) shr 8) - ((Color2 and $ff00) shr 8)) <= Tolerance) and
(Abs(((Color1 and $ff0000) shr 16) - ((Color2 and $ff0000) shr 16)) <= Tolerance));
end;

From: Pwnaz0r

I hoped you liked my tutorial, thanks for reading.

R0b0t1

2007-11-23 11:31:57

It looks nice, but I learned this (sorry for being an ass) about a month ago when BenLand told me to look at the Microsoft GDI documentation :p.

You've made me interested in making an autocoloring procedure using bit operations, but I have yet to find a way to do it :)

Bobarkinator

2007-11-23 23:28:00

From: R0b0t1

You've made me interested in making an autocoloring procedure using bit operations, but I have yet to find a way to do it :)


Me also, would be fun to do, and extremely fast.

Anyways, yet another great tutorial. Hope to see more.

R0b0t1

2007-11-24 00:13:06

Well, if you can get the bytes out of values you can do it.

$00BBGGRR <- just get the value and compare it. You can do it in C++, dunno bout delphi.