| | |
check the length of a hex number?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hey
I'm writing a color class, and I want to have a constructor that can take in an unsigned int like 0x00FF00FF and interpret it as fully opaque green. I have code for that (I think it should work, could someone check it?):
The thing is, I'd like the user to also be able to give this input: 0x00FF00, and have that also be interpreted as opaque green (alpha value would be FF by default), but I can't figure out how to do that. Is there a way to check the length of a number passed as input (or some other algorithm to do what I want). I can't just compare the values, because 0xffffff (opaque white) comes out to 16,777,215, but 0x0000ffee (slightly transparent green) comes out to 65,518, a smaller value, even though it's the the larger form.
I'm writing a color class, and I want to have a constructor that can take in an unsigned int like 0x00FF00FF and interpret it as fully opaque green. I have code for that (I think it should work, could someone check it?):
C++ Syntax (Toggle Plain Text)
rzb::color::color(unsigned int hex) { m_red = (hex & 0xff000000)/(0x00ffffff); m_green = (hex & 0x00ff0000)/(0x0000ffff); m_blue = (hex & 0x0000ff00)/(0x000000ff); m_alpha = (hex & 0x000000ff); }
The thing is, I'd like the user to also be able to give this input: 0x00FF00, and have that also be interpreted as opaque green (alpha value would be FF by default), but I can't figure out how to do that. Is there a way to check the length of a number passed as input (or some other algorithm to do what I want). I can't just compare the values, because 0xffffff (opaque white) comes out to 16,777,215, but 0x0000ffee (slightly transparent green) comes out to 65,518, a smaller value, even though it's the the larger form.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Wow, nvm - just figured it out.
Have alpha be first, not last.
But if someone could confirm that and/or check the above code I'd appreciate it.
Have alpha be first, not last.
But if someone could confirm that and/or check the above code I'd appreciate it.
Last edited by CoolGamer48; Aug 17th, 2008 at 11:10 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
•
•
•
•
Wow, nvm - just figured it out.
Have alpha be first, not last.
But if someone could confirm that and/or check the above code I'd appreciate it.
C++ Syntax (Toggle Plain Text)
unsigned int red = (hex & 0x00ff0000) >> 16;
Oh, you're right.
But actually, I dropped the system of using different members for red, green, blue, and alpha. I'm just using a single hex value as a member to save space.
Edit:
Just wondering, would this be any faster:
But actually, I dropped the system of using different members for red, green, blue, and alpha. I'm just using a single hex value as a member to save space.
Edit:
Just wondering, would this be any faster:
C++ Syntax (Toggle Plain Text)
unsigned int red = (hex << 8) >> 16; //or is it: unsigned int red = (hex << 8) >> 24;
Last edited by CoolGamer48; Aug 18th, 2008 at 1:21 am.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
•
•
•
•
Oh, you're right.
But actually, I dropped the system of using different members for red, green, blue, and alpha. I'm just using a single hex value as a member to save space.
Edit:
Just wondering, would this be any faster:
C++ Syntax (Toggle Plain Text)
unsigned int red = (hex << 8) >> 16; //or is it: unsigned int red = (hex << 8) >> 24;
C++ Syntax (Toggle Plain Text)
unsigned int red = (hex << 8) >> 24;
Which one is faster is a great question, one that I don't know the answer to but that hopefully others will.
Think about 64-bit portability (why not?):
Make user input argument safety in 100% portable manner, for example:
hex << 8) >> 24 does not work if sizeof(unsigned int) > 4.Make user input argument safety in 100% portable manner, for example:
C++ Syntax (Toggle Plain Text)
rzb::color::color(unsigned int hex) { m_alpha = (hex & 0xFF); m_blue = ((hex>>=8) & 0xFF); m_green = ((hex>>=8) & 0xFF); m_red = ((hex >>8) & 0xFF); }
Err... ok, ran into a prob again (like my first one). fyi ive gone back to the multiple member system (r,g,b,a as four separate variables). I still can't figure out how to let users pick whether or not to give the alpha bits. If they give (in a ARGB format) 0xee00ff00 (slightly transparent green), I'll read it fine, but if they give 0x00ff00, I'll read the alpha value as 0x00, when it should be 0xff. So my fix didn't really fix anything I guess. Any ideas on how to get this to work?
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
![]() |
Similar Threads
- Using strcpy and structs (C)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Introducing myself and a n00b help question. ;)
- Next Thread: Homework problem (can't figure it out)
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






