Okay, so I have a 16 bit positive value (0-65535) representing a color in 565 RGB notation, that is 5 bits for red, 6 bits for green, and 5 bits for blue.
I need to separate the red, green, and blue values out so I may give them to a function that takes red, green, and blue values separately.
So, thinking back to C, I figured I could use bitshifts. But when I do, it appears the bits wrap around. So is this even possible in Python?
8 1586
Okay, so I have a 16 bit positive value (0-65535) representing a color in 565 RGB notation, that is 5 bits for red, 6 bits for green, and 5 bits for blue.
I need to separate the red, green, and blue values out so I may give them to a function that takes red, green, and blue values separately.
So, thinking back to C, I figured I could use bitshifts. But when I do, it appears the bits wrap around. So is this even possible in Python?
One way is (off the top of my head) -
-
rgbValue = 65535
-
red = rgbValue & 0xf800 >> 11
-
green = rgbValue & 0x07e0 >> 5
-
blue = rgbValue & 0x001f
-
I'm sure I'll think of others when the pressure is off.
By the way, spacecoyote, welcome to TheScripts! We're glad you found the site and hope that you post often and tell your friends about us.
One way is (off the top of my head) -
-
rgbValue = 65535
-
red = rgbValue & 0xf800 >> 11
-
green = rgbValue & 0x07e0 >> 5
-
blue = rgbValue & 0x001f
-
I'm sure I'll think of others when the pressure is off.
Unfortunately, that doesn't seem to work :(
If I substitute (for example) the value 43039, r, g, and b still equal 31, so it apparently only works for 65535...
What values are you expecting? 31 is 011111 (5 bits all one).
Hmm...
43039 = 10101000000 11111
so:
r=10101=21
g=000000=0
b=11111=31
I see that shifting doesn't work for some reason. I'll research this and get back to you.
The AND part works, so for now you can just divide by the appropriate power of 2
r = c >> 11 works and
b = c & 0x001f works,
g is the tough one...
r = c >> 11 works and
b = c & 0x001f works,
g is the tough one...
Boy do I feel foolish. It's operator presidence:
(43039 & 0x07e0) >> 5
Boy do I feel foolish. It's operator presidence:
(43039 & 0x07e0) >> 5
Hooray! It works. Thank you :)
Hooray! It works. Thank you :)
You are quite welcome. Post any time.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dave |
last post by:
After following Microsofts admonition to reformat my system before doing a
final compilation of my app I got many warnings/errors upon compiling an...
|
by: Jacek Generowicz |
last post by:
Where can I find concise, clear documentation describing what one has
to do in order to enable Python's internal help to be able to provide...
|
by: wukexin |
last post by:
Help me, good men. I find mang books that introduce bit "mang header
files",they talk too bit,in fact it is my too fool, I don't learn it, I have...
|
by: Colin J. Williams |
last post by:
Python advertises some basic service:
C:\Python24>python
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on
win32
Type "help", "copyright",...
|
by: Corepaul |
last post by:
Missing Help Files
When I enter "recordset" as the keyword and search the Visual Basic Help index,
I get many topics of interest in the resulting...
|
by: Steve |
last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp
My expectation is that a developer using my DLL would be able to...
|
by: Mark |
last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my
home computer has an abbreviated help screen not 2% of the help on my...
|
by: JonathanOrlev |
last post by:
Hello everybody,
I wrote this comment in another message of mine, but decided to post it
again as a standalone message.
I think that...
|
by: trunxnirvana007 |
last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more:...
|
by: hitencontractor |
last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003.
I added a menu item called "MyApp Help" in the end of the menu...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |