PDA

Click to See Complete Forum and Search --> : Html -> Rgb


PsychoMark
Jul 6th, 2001, 01:01 PM
Ok, I've got this piece of code to convert a color value to HTML coding (it's Delphi code, but it's fairly easy to understand for any VB coder):


iRed := Color and $FF;
iGreen := (Color and $FF00) div 256;
iBlue := (Color and $FF0000) div 65536;

Result := '#' + IntToHex(iRed, 2) + IntToHex(iGreen, 2) + IntToHex(iBlue, 2);


The only differences are that in VB $ = &H and IntToHex() is Hex()...


Ok, now that works perfectly, but my problem is, I want to reverse it. I've searched half the internet, but couldn't find it. Does anyone know how it works? The code doesn't have to be Delphi code, it can just be VB code...

Thanks!

kedaman
Jul 6th, 2001, 02:35 PM
since html color is bgr, it's just the reverse rgb, so bgr is rgb reversed.

PsychoMark
Jul 6th, 2001, 02:40 PM
HTML is RGB, but that's not really what I meant.

Say I have this value, "#FF0000" (Red), now I want to get the individual RGB values from this, which would be FF, 00 and 00, but how do I convert this to 0-255 range so I would get 255, 0, 0?

Offcourse I can do it manually with 100 if's, but I hate that :)

kedaman
Jul 6th, 2001, 02:52 PM
It looks like it's RGB but in fact the hi byte is in the left end, not the right.

type

?&hff0000

in immediate window and it'll result, 16711680. the code for blue, not red.

I'm not familiar with delphi, so you must excuse me. in vb you'd do val("&h" & hexvalue)
If there's no built in function for this, you don't need too many if's to accomplish the same tasks. loop trough each byte from hi to low, add up the ascii-48 and substract 7 for the range A-F. then you increment the byte pointer and multiply itself with the radix(16)

PsychoMark
Jul 6th, 2001, 02:57 PM
Mhhhh........I'll go for the if-approach ;) (not good at math)

But if anyone has some VB code I can understand (I understand things better when I look at code then when someone tries to explain it :)), converting it shouldn't be a problem...

kedaman
Jul 6th, 2001, 03:20 PM
val() function knows what to do but as said i have no idea with delphi. some pseudo for what i was trying to say:

byte x, result y
loop x from hibyte to lowbyte
if x<58
y=y+x-48
else
y=y+x-55
y*=16

PsychoMark
Jul 6th, 2001, 03:23 PM
Still don't quite understand it, but thanks anyways. I'll work something out...

kedaman
Jul 6th, 2001, 03:26 PM
just ask if you get stuck :p

denniswrenn
Jul 6th, 2001, 05:56 PM
Maybe you can convert this to Delphi... It uses quite a bit of If's, but not 100 ;)

Private Sub Form_Load()
Dim a As Byte, b As Byte, c As Byte
ToRGB "#FFFFFF", a, b, c
MsgBox a
MsgBox b
MsgBox c
End Sub

Private Sub ToRGB(ByVal sHex As String, ByRef bR As Byte, ByRef bG As Byte, ByRef bB As Byte)
Dim sR As String
Dim sG As String
Dim sB As String
Dim sR1 As String
Dim sR2 As String
Dim sG1 As String
Dim sG2 As String
Dim sB1 As String
Dim sB2 As String

sHex = UCase(sHex)
If Left(sHex, 1) = "#" And Len(sHex) = 7 Then
sR = Mid(sHex, 2, 2)
sG = Mid(sHex, 4, 2)
sB = Mid(sHex, 6, 2)
ElseIf Len(sHex) = 6 Then
sR = Mid(sHex, 1, 2)
sG = Mid(sHex, 3, 2)
sB = Mid(sHex, 5, 2)
End If

sR1 = Chr(Asc(Left(sR, 1)))
sR2 = Chr(Asc(Right(sR, 1)))
sG1 = Chr(Asc(Left(sG, 1)))
sG2 = Chr(Asc(Right(sG, 1)))
sB1 = Chr(Asc(Left(sB, 1)))
sB2 = Chr(Asc(Right(sB, 1)))
'RED
If IsNum(sR1) Then
bR = (Asc(sR1) - 48) * 16
Else
bR = (Asc(sR1) - 55) * 16
End If
If IsNum(sR2) Then
bR = bR + (Asc(sR2) - 48)
Else
bR = bR + (Asc(sR2) - 55)
End If
'GREEN
If IsNum(sG1) Then
bG = (Asc(sG1) - 48) * 16
Else
bG = (Asc(sG1) - 55) * 16
End If
If IsNum(sG2) Then
bG = bG + (Asc(sG2) - 48)
Else
bG = bG + (Asc(sG2) - 55)
End If
'BLUE
If IsNum(sB1) Then
bB = (Asc(sB1) - 48) * 16
Else
bB = (Asc(sB1) - 55) * 16
End If
If IsNum(sB2) Then
bB = bB + (Asc(sB2) - 48)
Else
bB = bB + (Asc(sB2) - 55)
End If
End Sub

Private Function IsNum(sNum As String) As Boolean
If Len(sNum) <> 1 Then
IsNum = False
ElseIf Asc(sNum) >= 48 And Asc(sNum) <= 57 Then
IsNum = True
End If
End Function

PsychoMark
Jul 7th, 2001, 03:56 AM
Thank you!! That worked great!

I know this is a VB forum (really? I didn't even notice that? Wow... ;)), but here's the Delphi code anyways for anyone interested:



function IsNum(Value: Char): Boolean;
begin
// Check ASCII value
if (Ord(Value) >= 48) and (Ord(Value) <= 57) then
Result := True
else
Result := False;
end;

function ConvertValue(Value: String): Byte;
var
bResult: Byte;

begin
// Check first character
if IsNum(Value[1]) then
bResult := (Ord(Value[1]) - 48) * 16
else
bResult := (Ord(Value[1]) - 55) * 16;

// Check second character
if IsNum(Value[2]) then
bResult := bResult + (Ord(Value[2]) - 48)
else
bResult := bResult + (Ord(Value[2]) - 55);

Result := bResult;
end;

function HTMLtoRGB(HTML: String): TColor;
var
sHTML: String;
sR,
sG,
sB: String;
bR,
bG,
bB: Byte;
iOffset: Integer;
iLength: Integer;

begin
// #ff0000 -> #FF0000 (necessary for ASCII operations)
sHTML := UpperCase(HTML);

// Check length
iLength := Length(sHTML);

if (iLength < 6) or (iLength > 7) then begin
Result := 0;
exit;
end;

if (sHTML[1] = '#') and (Length(sHTML) = 7) then
iOffset := 2
else
iOffset := 1;

// Get RGB values (using the 'string = array' rule)
sR := sHTML[iOffset] + sHTML[iOffset + 1];
sG := sHTML[iOffset + 2] + sHTML[iOffset + 3];
sB := sHTML[iOffset + 4] + sHTML[iOffset + 5];

// Convert RGB values
bR := ConvertValue(sR);
bG := ConvertValue(sG);
bB := ConvertValue(sB);

// Return color
Result := RGB(bR, bG, bB);
end;

denniswrenn
Jul 7th, 2001, 04:07 AM
Wow, I must say, that is one effed up language ;)

I'd prefer C++ anyday :)

Any particular reason you're coding in delphi rather than something like C++?(just curious)

PsychoMark
Jul 7th, 2001, 04:20 AM
Well, there are some reasons why I moved from VB to Delphi, and some why I didn't choose C++.

First of all, Delphi looks more like VB than C++. I initially did start to learn C++, created a DLL, but it takes a lot of code to create a window (unless you're using MFC, but that would still require a DLL, and that's the main reason I stopped using VB :)). Delphi however still has the possibilities to create a Window using pure API, but also has a lot of standard components which are merged with the EXE (resulting in a 300 KB EXE file, but that isn't really that bad compared to the at least 2 MB of runtimes which VB needs)... but I created an OpenGL application recently which did create a window using only API, and it turned out to be 20 KB :D

Also, Delphi's IDE takes more time to load, but compiling a program is done within seconds (and that's on my P133 :p)...

And last but not least, the speed of Delphi compared to the speed of C++ is not that much. I wouldn't say it's just as fast, but it comes close... and if that's not enough, Delphi also supports inline assembly...

Now looking at the speed at which the programs run in both languages, compared to the time it takes to produce such a program, that made me choose Delphi...


(oh, and the help files which come with Delphi are incredible!! they're huge!! not only the Delphi help is included, but also a lot of MS SDK help files, like all the API function and OpenGL reference)

PsychoMark
Jul 7th, 2001, 04:23 AM
"Wow, I must say, that is one effed up language"

What did you mean with that anyways?

denniswrenn
Jul 7th, 2001, 04:24 AM
Well, you can use dialog's in C++.... MSVC++ has a dialog editor... it's like creating something in VB or Delphi(it's visual), but it results in an executable that is still horrifyingly small :)

But, I don't really like using dialogs... CreateWindowEx == :cool: :rolleyes:

kedaman
Jul 7th, 2001, 10:08 AM
Delphi looks much like pascal right? (begin and end everywhere) Isn't it even to pascal something VB to basic?
Originally posted by PsychoMark
Delphi however still has the possibilities to create a Window using pure API, but also has a lot of standard components which are merged with the EXE (resulting in a 300 KB EXE file, but that isn't really that bad compared to the at least 2 MB of runtimes which VB needs)
Yeah, you can do that in VB too, I remember making a unattended execution dll which used createwindowex. The VB runtimes are part of windows scripting so there's no need to include them in your package. Anyways, C++ horrifying small executables rock :D

PsychoMark
Jul 7th, 2001, 10:27 AM
1. Delphi = Object Pascal + VCL (Visual Components Library, all the controls which are merged with the EXE)

2. VB can do that, but it would still require the runtime DLL's. Even though the runtimes are included with the OS more often, users running for example Win95 would still need them... (and I must say that the AddressOf thing in VB isn't exactly the most stable :))

3. Yeah, offcourse C++ is the king when it comes to small executables and high speeds, but like I said, I don't want to spent 20 minutes just to add a textbox...


4. I don't hate C++ or anything, I think it's a great language! It's just not my primary language...

denniswrenn
Jul 7th, 2001, 01:34 PM
*ahem*

>>>>Well, you can use dialog's in C++.... MSVC++ has a dialog editor... it's like creating something in VB or Delphi(it's visual), but it results in an executable that is still horrifyingly small :) <<<<
:p

PsychoMark
Jul 7th, 2001, 01:41 PM
Oops, didn't notice that one... anyways, I still like Delphi a little bit more :)

denniswrenn
Jul 7th, 2001, 01:44 PM
Originally posted by PsychoMark
"Wow, I must say, that is one effed up language"

What did you mean with that anyways?

effed = fed = f***ed

Was just kidding though :p Well, kind of; pascal is wierd..

kedaman
Jul 7th, 2001, 06:45 PM
I agree with dennis, pascal is weird, but i've heard it's a very nice and well structured language. Can you use pointers? With Object Pascal, do you mean the objects are all COM? Is it ActiveX compatible?

PsychoMark
Jul 8th, 2001, 04:43 AM
Well, I don't think it's weird, but that's just a matter of getting used to it....

It is in my opinion very well structured. The way the objects are built, it's very well done. Delphi does support Pointers (really supports, not like VB). Delphi also supports COM and ActiveX, but I don't think that's what they mean with Object Pascal. I think it has something to do with the structure of the objects, they all use a base class which they inherit properties from. You can do some nice things with that btw...

Delphi also supports things like overloading functions, built-in message handling support (just add 'message WM_PAINT' for example after you've declared a function, and it reacts to it.... at least when you declare it in a form), and inline assembly (I already mentioned that, didn't I? :rolleyes: )

Name something that can be done in C++, and I'll bet it can be done in Delphi too...



Oops, one thing I like about C++: "X++" or "X *= 40". Delphi doesn't support that :(. It does however has the "Inc(X)" function for integers, which the compilers optimizes a lot more than "X := X + 1"...

PsychoMark
Jul 8th, 2001, 04:48 AM
*looks at the other thread of denniswrenn*

...and Delphi also support threading, both using the API and the supplied TThread objecty :D

kedaman
Jul 8th, 2001, 01:55 PM
Sounds very nice, if you compare to vb6. templates? if not inheritance and polymorphism i guess templates. Inline asm sounds good, but other than that the variety of operators, some low level ones not only ++ and assignment operators, what about bitshifting (<< >> <<= >>=)? and operator overloading, maybe a bit unnessesary but cool feature, you can actually replace all methods and go wild on polymorphism with object only programming ;) Is there a preprocessor in object pascal? You know #include #define and company :p Further on inheritance, multiple inhertinace? virtual inheritance? virtual functions? what about type structures, can they functions as objects like in c++, and I don't mean COM ones.

PsychoMark
Jul 8th, 2001, 02:10 PM
Wow, that's a lot of questions. Actually I don't really know, I've only worked with Delphi for about a month now, and I'm not really doing things like bitshifting :)

You should check out borland.com or something for those kind of questions, I just know I like Delphi and that it can do a lot more than VB at a much higher speed :)

kedaman
Jul 8th, 2001, 02:16 PM
maybe, or maybe i'll stay with c++. pascal just looks weird ;)

denniswrenn
Jul 8th, 2001, 02:19 PM
C++ Rules..!

But I installed Delphi anyway(free copy of Ver. 4 standard with C++ Builder 5 pro) just incase garfield needs help ;) :rolleyes:

:)

Jotaf98
Jul 9th, 2001, 02:33 PM
Hey, I have a question: what do you think is better, C++ or ASM?

Because I wanna learn one of them but I don't know which; also I don't know where to start learning it :)

PsychoMark
Jul 9th, 2001, 02:38 PM
I think C++. It's fast enough, allows for more rapid development, but still allows ASM to be used in case speed is really the top priority...

parksie
Jul 9th, 2001, 02:49 PM
C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++!
BURN IN HELL ALL NON-C++ USERS!!!!! BWAHAHAHAHAHAHA :D

I hate Pascal :) I don't like the way it has keywords for things you don't need (begin/end) and also the separation of variables from code is nasty.

PsychoMark
Jul 9th, 2001, 02:53 PM
Is Parksie trying to make a point? :D



(burn in hell all you "burn in hell people who don't use [your language here]" !! :p)

parksie
Jul 9th, 2001, 02:55 PM
Me? Point? Naaaaahhhh :p

kedaman
Jul 9th, 2001, 02:59 PM
Originally posted by parksie
C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++! C++!
BURN IN HELL ALL NON-C++ USERS!!!!! BWAHAHAHAHAHAHA :D

I hate Pascal :) I don't like the way it has keywords for things you don't need (begin/end) and also the separation of variables from code is nasty. exactly, being and end all the time instead of {} makes you sick :p C++ for the real pro's, VB for kids and pascal for other weirdos

parksie
Jul 9th, 2001, 03:04 PM
The neat thing with C++ or C is that you can really strip it down to the bare limit - you can get a 32-bit C program in under a kilobyte by using no standard libraries and implementing all the code yourself (including _mainCRTStartup).

Jotaf98
Jul 9th, 2001, 06:17 PM
Hehe I tricked you all, I've been learning ASM for a long time to optimize my special effects module (it's getting kinda hard now hehe) and it rocks, check this out ;)

Jotaf98
Jul 9th, 2001, 06:23 PM
Btw look at the size of the EXE, it's HIGHLY optimized!

PsychoMark
Jul 10th, 2001, 01:57 AM
Originally posted by kedaman
exactly, being and end all the time instead of {} makes you sick :p C++ for the real pro's, VB for kids and pascal for other weirdos


Well, in that case I'm a weirdo! :D

To Jotaf98, can you post the code used to do the messagebox?

I tried to get a program as small as possible in Delphi too, it turned out to be 16 kb (mainly because of the System unit Delphi automatically adds), but at least the code was readible:


program msgbox;

const
{ MessageBox constants }
MB_OK = $00000000;
MB_ICONINFORMATION = $00000040;

{ MessageBox API }
function MessageBox(hWnd: LongWord; lpText, lpCaption: PChar; uType: LongWord): Integer;
stdcall; external 'user32.dll' name 'MessageBoxA';

begin
MessageBox(0, 'Delphi kicks ass!', ':p', MB_OK or MB_ICONINFORMATION);
end.



(btw, the program to color the code, made that in Delphi, took me 2 hours, including preview option and nice looking interface, now try that in ASM :p)



Btw, where did you learn ASM? Since Delphi supports the 'asm' keyword, I would sure like to try it out some time...



note to self: add line-break option for code :)

PsychoMark
Jul 10th, 2001, 09:54 AM
Oops, nevermind the "I want to learn ASM part"...


Just looked at a tutorial to show a MessageBox... :eek: *runs away screaming and pulling his hair out*



I'll stick with Delphi for a while :rolleyes:
(just finished my first OpenGL test, OpenGL rules!! :D)

Jotaf98
Jul 11th, 2001, 05:50 PM
Hehe, in fact, that's the program that comes with the sample winzip file, modified a bit with my Hex Editor :)

Do you know any good books about C++? Whenever someone reccomends me a book, someone else says that book s*cks... :(

And the only part that I wanna learn is DLLs and dynamic 2 and 3 dimensioned arrays :)

kedaman
Jul 12th, 2001, 02:27 PM
If that's the only thing you want to learn, that's probably not where you'll end up.
There's no built in "arrays" in C++, you use pointers, and you'll learn to love them or you'll just quit C++. There are of course array classes but I think they suck. exporting DLL functions shouldn't be that hard, in fact that was the first thing I wanted to do when I started C++ and now it's the last thing I want to learn :p

I still do recommend Sam's teach yourself C++ in 21 days, it took about 5 days for me to read trough.

PsychoMark
Jul 12th, 2001, 02:58 PM
*cough* Delphi *cough* :D



Well, actually, that's exactly what I wanted to use C++ for too. I only looked at the sample at DirectX4VB, and did get it working, but that's about it... still addicted to pointers though :p

I did download a tutorial for C++ (CyberCarsten's site if I'm correct), which wasn't specifically about DLL's and arrays, but it does help learning the language, so I'll go with kedaman's advice...

Jotaf98
Jul 13th, 2001, 08:37 AM
Hum I looked at that tutorial too. How do I work with 3-dimensions arrays then? :)

kedaman
Jul 13th, 2001, 02:13 PM
i'd do it this way (multidimensioned arrays are singledimensioned arrays in a way)
*(first+index1+index2*dim1+index3*dim2*dim1)

others might do

*first[index1+index2*dim1+index3*dim2*dim1]

if you use static arrays, you can declare them as follows:

datatype myarray[6,4,3]

and refer to element i1,i2,i3 with

myarray[i1,i2,i3]

you can use this multidimensional expression only if the arrays are static, since the compiler will know the dimensions and fill them in.

Jotaf98
Jul 14th, 2001, 12:48 PM
Hum, I think I'll use the first one you suggested ;)

If I wanted to do something that in VB would look like this:

Array1(x,y) = Array2(x,y,z)

How could I do it in C++? The asterisk only lets me point to one at a time, right?

Zaei
Jul 14th, 2001, 01:40 PM
C++ does indeed rock. What other language can do this?


#include <iostream.h>
#define Begin void main() {
#define end }
#define and &&
#define if if(
#define then )
#define writeln(x) cout << x << endl;

Begin
if (1 < 2) and (2 < 3) then writeln("Hello World!");
end


And yes.... It runs... mwuahahahahah.

Z.

Jotaf98
Jul 14th, 2001, 02:02 PM
Cool, Zaei!

Anyway I think I figured it out... all I'd have to do would be use the first element of the array as "first" in the formulae, right?

kedaman
Jul 14th, 2001, 06:08 PM
i think it's better to stay with the orignal language, others might wonder what kind of programmer you really are :p

the asterix is used to dereference the pointer, so if you want to assign the element, dereference it

Zaei
Jul 14th, 2001, 08:10 PM
Yeah. You can do some whacky stuff with #define...

#include <iostream.h>

#define This void main() {
#define code int x; cout << "Enter a value: ";
#define is cin >> x;
#define really if(x <= 5) cout << x << endl;
#define very if(x > 5) cout << x +1 << endl;
#define wierd }

This code is really very wierd


Z.

Jotaf98
Jul 17th, 2001, 12:42 PM
???:confused:???

You got me lost on that one, keda! What do you mean?

PsychoMark
Jul 17th, 2001, 12:45 PM
*testing my own knowledge*

do you mean like when you have a pointer to an object, you get the object data back from the pointer?

(did I pass? :D)

Jotaf98
Jul 17th, 2001, 01:36 PM
Heh, maybe that's it, because I was passing it the first element of the array and I wanna read from that position on...

kedaman
Jul 17th, 2001, 04:39 PM
PsychoMark knowledge test :) Yes, that's correct.

if you have a pointer to a variable it contains the address of the variable, not the object itself, the pointer is usually 4 bytes. If you do operations directly on the pointer, the address will change, for instance
int[4] array={1,2,3,4};
int* ptr=int[2];
cout << ptr << endl; //shows an address
cout << *ptr << endl; //shows 3
ptr--; //moves pointer backwards
cout << *ptr << endl; // shows 2
ptr+=2; //moves pointer forward 2 steps
cout << *ptr << endl; // shows 4

PsychoMark
Jul 18th, 2001, 03:52 AM
Whoo hoo! Now where's the car I just won? :p

Jotaf98
Jul 20th, 2001, 07:00 PM
Look, I just need some working code for a 3-d array, ok? :)

parksie
Jul 20th, 2001, 07:06 PM
long x[3][3][3];

x[0][0][0] = 0;
x[0][2][1] = 5;

Jotaf98
Jul 22nd, 2001, 09:49 AM
Thanks Parksie - but the only problem is that the array is passed to it in the function's arguments, and I don't know its exact size :(

Anyway, I already found someone who will code it in Asm! But he's still not sure if he's gonna make it so thanks for your help (I might need it later) ;)

kedaman
Jul 22nd, 2001, 11:15 AM
so go with pointers jotaf ;) that is in case your asm assistant decides to dump you (who is it if i may ask?) :)