|
-
Jul 6th, 2001, 01:01 PM
#1
Thread Starter
Fanatic Member
Html -> Rgb
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):
Code:
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!
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 6th, 2001, 02:35 PM
#2
transcendental analytic
use the same function
since html color is bgr, it's just the reverse rgb, so bgr is rgb reversed.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 6th, 2001, 02:40 PM
#3
Thread Starter
Fanatic Member
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
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 6th, 2001, 02:52 PM
#4
transcendental analytic
Illusion
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)
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 6th, 2001, 02:57 PM
#5
Thread Starter
Fanatic Member
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...
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 6th, 2001, 03:20 PM
#6
transcendental analytic
val() function knows what to do but as said i have no idea with delphi. some pseudo for what i was trying to say:
Code:
byte x, result y
loop x from hibyte to lowbyte
if x<58
y=y+x-48
else
y=y+x-55
y*=16
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 6th, 2001, 03:23 PM
#7
Thread Starter
Fanatic Member
Still don't quite understand it, but thanks anyways. I'll work something out...
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 6th, 2001, 03:26 PM
#8
transcendental analytic
just ask if you get stuck
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 6th, 2001, 05:56 PM
#9
Maybe you can convert this to Delphi... It uses quite a bit of If's, but not 100 
VB Code:
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
-
Jul 7th, 2001, 03:56 AM
#10
Thread Starter
Fanatic Member
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:
Code:
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;
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 7th, 2001, 04:07 AM
#11
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)
-
Jul 7th, 2001, 04:20 AM
#12
Thread Starter
Fanatic Member
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 
Also, Delphi's IDE takes more time to load, but compiling a program is done within seconds (and that's on my P133 )...
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)
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 7th, 2001, 04:23 AM
#13
Thread Starter
Fanatic Member
"Wow, I must say, that is one effed up language"
What did you mean with that anyways?
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 7th, 2001, 04:24 AM
#14
-
Jul 7th, 2001, 10:08 AM
#15
transcendental analytic
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 7th, 2001, 10:27 AM
#16
Thread Starter
Fanatic Member
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...
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 7th, 2001, 01:34 PM
#17
*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 <<<<
-
Jul 7th, 2001, 01:41 PM
#18
Thread Starter
Fanatic Member
Oops, didn't notice that one... anyways, I still like Delphi a little bit more
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 7th, 2001, 01:44 PM
#19
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 Well, kind of; pascal is wierd..
-
Jul 7th, 2001, 06:45 PM
#20
transcendental analytic
I knew it, those := was looking very pascalish to me
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?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 8th, 2001, 04:43 AM
#21
Thread Starter
Fanatic Member
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? )
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"...
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 8th, 2001, 04:48 AM
#22
Thread Starter
Fanatic Member
*looks at the other thread of denniswrenn*
...and Delphi also support threading, both using the API and the supplied TThread objecty
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 8th, 2001, 01:55 PM
#23
transcendental analytic
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 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.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 8th, 2001, 02:10 PM
#24
Thread Starter
Fanatic Member
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
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 8th, 2001, 02:16 PM
#25
transcendental analytic
maybe, or maybe i'll stay with c++. pascal just looks weird
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 8th, 2001, 02:19 PM
#26
-
Jul 9th, 2001, 02:33 PM
#27
Frenzied Member
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
-
Jul 9th, 2001, 02:38 PM
#28
Thread Starter
Fanatic Member
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...
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 9th, 2001, 02:49 PM
#29
Monday Morning Lunatic
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 
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 9th, 2001, 02:53 PM
#30
Thread Starter
Fanatic Member
Is Parksie trying to make a point? 
(burn in hell all you "burn in hell people who don't use [your language here]" !! )
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 9th, 2001, 02:55 PM
#31
Monday Morning Lunatic
Me? Point? Naaaaahhhh
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 9th, 2001, 02:59 PM
#32
transcendental analytic
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 9th, 2001, 03:04 PM
#33
Monday Morning Lunatic
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).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 9th, 2001, 06:17 PM
#34
-
Jul 9th, 2001, 06:23 PM
#35
Frenzied Member
Btw look at the size of the EXE, it's HIGHLY optimized!
-
Jul 10th, 2001, 01:57 AM
#36
Thread Starter
Fanatic Member
Originally posted by kedaman
exactly, being and end all the time instead of {} makes you sick C++ for the real pro's, VB for kids and pascal for other weirdos
Well, in that case I'm a weirdo! 
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:
Code:
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 )
Btw, where did you learn ASM? Since Delphi supports the 'asm' keyword, I would sure like to try it out some time...
[edit] note to self: add line-break option for code [/edit]
Last edited by PsychoMark; Jul 10th, 2001 at 02:00 AM.
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
-
Jul 10th, 2001, 09:54 AM
#37
Thread Starter
Fanatic Member
-
Jul 11th, 2001, 05:50 PM
#38
Frenzied Member
-
Jul 12th, 2001, 02:27 PM
#39
transcendental analytic
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 
I still do recommend Sam's teach yourself C++ in 21 days, it took about 5 days for me to read trough.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 12th, 2001, 02:58 PM
#40
Thread Starter
Fanatic Member
*cough* Delphi *cough* 
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 
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...
Teaudirenopossum.Musasapientumfixaestinaure.
(I can't hear you. There's a banana in my ear)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|