|
-
Nov 13th, 2002, 04:53 PM
#1
Thread Starter
Frenzied Member
Convert HTML colour codes to
I have this code that works in VB for converting an html colour string into a visual basic colour. How can I do the same thing in C??????
Keep in mind that RGB does not work in C so I would need an equivalent function for this also.
VB Code:
'Convert a colour represented in html colour format,
'to a long value recognizable by visual basic
Function HTMLToLong(sHTML As String) As Long
Dim lR As Long, lB As Long, lG As Long
lR = CInt(Val("&h" + Mid(sHTML, 2, 2)))
lG = CInt(Val("&h" + Mid(sHTML, 4, 2)))
lB = CInt(Val("&h" + Mid(sHTML, 6, 2)))
HTMLToLong = RGB(lR, lG, lB)
End Function
-
Nov 13th, 2002, 09:08 PM
#2
Good Ol' Platypus
I don't know C, so this may only be valid in C++... hell, I'm not that good, so it may not be valid at all... 
PHP Code:
long int HTMLToLong(char* sHTML[]) {
long int lR; long int lB; long int lG;
lR = (int)...
lG = (int)...
lB = (int)...
return lR + lG * 256 + lB * 65536;
}
I'm not sure how to do those middle parts, so I'll let a 'guru' fill those out, sorry.
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Nov 13th, 2002, 10:11 PM
#3
Frenzied Member
There is an RGB macro availible.
Code:
#define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \
((WORD) (g) << 8)) | \
(((DWORD) (BYTE) (b)) << 16)))
From MSDN.
As for the hex code to int... this should work:
Code:
char hex_byte_string_to_byte(char* code)
{
int val[i];
for(int i = 0; i < 2; ++i)
{
if((code[i] >= '0') && (code[i] <= '9'))
{
val[i] = code[i] - '0';
}
else
{
val[i] = code[i] - 'A' + 10;
}
}
return val[0] << 4 + val[1];
}
Seems correct, but I didnt test it.
Z.
-
Nov 14th, 2002, 07:11 PM
#4
Good Ol' Platypus
As I was taking a walk today, I realised that C has bitshifts in it. D'oh! So I was going to let you know, but of course Zaei beat me to it
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Nov 15th, 2002, 07:54 AM
#5
There goes: converts any #xxxxxx string to a COLORREF:
Code:
#include <string>
#include <sstream>
COLORREF ReadHtmlColor(const string& str)
{
istringstream iss(str);
iss.ignore(1);
int r, g, b;
iss >> setbase(16) >> r >> g >> b;
return RGB(r, g, b);
}
Should work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 15th, 2002, 08:53 AM
#6
Note however that HTML colors can also be in the format #xxx, where each color component is represented by a single hex digit. It's equivalent to writing #x0x0x0. Here's a function that can read this too.
Oh, and I noticed my function won't work, here's a corrected and complete version.
Code:
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
COLORREF ReadHtmlColor(const string& str)
{
istringstream iss;
int r, g, b;
if(str.length() == 4) {
iss.str((str.substr(1,1)));
iss >> setbase(16) >> r;
r <<= 8;
iss.str((str.substr(2,1)));
iss >> setbase(16) >> g;
g <<= 8;
iss.str((str.substr(3,1)));
iss >> setbase(16) >> b;
b <<= 8;
} else if(str.length() == 7) {
iss.str((str.substr(1,2)));
iss >> setbase(16) >> r;
iss.str((str.substr(3,2)));
iss >> setbase(16) >> g;
iss.str((str.substr(5,2)));
iss >> setbase(16) >> b;
} else return 0;
return RGB(r,g,b);
}
There's probably a more beautiful method.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 15th, 2002, 09:43 AM
#7
Frenzied Member
Originally posted by CornedBee
Should work.
I use this line all the time =).
My function does work (just tested it), except you need to change "int val[i]" to "int val[2]".
Z.
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
|