473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C - Convert RGB(31,31,31) to 16 bit colour.

1. Write a function or macro, which takes three values between 0 and 31, and converts this to BGR 16 bit format. For example, the function prototype should look like u16 BGR(u16 b, u16 g, u16 r);
That's the assigment from my teacher, but i think in 'normal' terms that means:

Write a function that takes RGB(r,g,b); r, g and b being values between 0 and 31; it then converts it to 16 bit RGB.

I haven't a clue how to do that, or what it means.

Thanks in advance.
Nov 18 '07 #1
16 4334
JosAH
11,448 Expert 8TB
That's the assigment from my teacher, but i think in 'normal' terms that means:

Write a function that takes RGB(r,g,b); r, g and b being values between 0 and 31; it then converts it to 16 bit RGB.

I haven't a clue how to do that, or what it means.

Thanks in advance.
Note that a value in the range [0, 31] takes up 5 bits. Three of them next to each
other take up 15 bits. That fits nicely in a 16 bit word. Have a look at the bit shift
operators << and >>

kind regards,

Jos
Nov 18 '07 #2
Note that a value in the range [0, 31] takes up 5 bits. Three of them next to each
other take up 15 bits. That fits nicely in a 16 bit word. Have a look at the bit shift
operators << and >>

kind regards,

Jos
That doesn't really mean much to me. I haven't done much with bits, bit shift etc..
Nov 18 '07 #3
JosAH
11,448 Expert 8TB
That doesn't really mean much to me. I haven't done much with bits, bit shift etc..
Well: start reading then.

kind regards,

Jos
Nov 18 '07 #4
Well: start reading then.

kind regards,

Jos
It would help if you could sort of lead me in the right direction.
Nov 18 '07 #5
JosAH
11,448 Expert 8TB
It would help if you could sort of lead me in the right direction.
That was exactly what I was doing: you have to solve the problem how to fit
three 5 bit numbers in one 16 bit number (there'll be one bit left then).

kind regards,

Jos
Nov 18 '07 #6
That was exactly what I was doing: you have to solve the problem how to fit
three 5 bit numbers in one 16 bit number (there'll be one bit left then).

kind regards,

Jos
Thanks, but i know what i have to do. The question was and still is, how do i do it?
Nov 18 '07 #7
JosAH
11,448 Expert 8TB
Thanks, but i know what i have to do. The question was and still is, how do i do it?
Well I gave you a hint already: read about the bit shift operators << and >>
but you simply said you know nothing about them, on which I replied that you
should read up on them. You don't want to keep this going on ad nauseam do you?

kind regards,

Jos
Nov 18 '07 #8
Well I gave you a hint already: read about the bit shift operators << and >>
but you simply said you know nothing about them, on which I replied that you
should read up on them. You don't want to keep this going on ad nauseam do you?

kind regards,

Jos
So basically:

Shifting left using << causes 0's to be shifted from the least significant end (the right side), and causes bits to fall off from the most significant end (the left side). This occurs whether the variable is signed or unsigned.
Shifting left by K bits is equivalent to multiplying by 2^K.
Shifting right using >> causes 0's to be shifted from the most significant end (the left side), and causes bits to fall off from the least significant end (the right side) if the number is unsigned. If it's signed, then it's implementation depedendent. It may shift in the sign bit from the left, or it may shift in 0's (it makes more sense to shift in the sign bit).
For unsigned int, and sometimes for signed int, shifting right by K bits is equivalent to dividing by 2^K (using integer division).
Bitshifting doesn't change the value of the variable being shifted. Instead, a temporary value is created with the bitshifted result.
So basically i have to change 31 into a 5 bit number? This is really confusing me.
Nov 18 '07 #9
AHMEDYO
112 100+
HI...

look 31 is basically 5 bits in memory, you was defined R,G, and B as integer thats mean each one holds in 16 bits,you must take only 5 bits fom each 16 and then merge '3 5bits in one integer variable

GOOD LUCK
Nov 18 '07 #10
JosAH
11,448 Expert 8TB
So basically:



So basically i have to change 31 into a 5 bit number? This is really confusing me.
You do realize that any value in the range [0,31] takes up only 5 bits do you?

kind regards,

Jos
Nov 18 '07 #11
oler1s
671 Expert 512MB
You should let us know if you are having trouble understanding the fundamental concept of numbers and their representation as bits. If the idea that a number from 0-31 takes only 5 bits is not instantaneously intuitive, then ask us why.

The syntax for this problem is very easy. You only need to know about bit shifts. The concepts are also easy, but if you don't understand the concept, you can't write the few lines of code.
Nov 18 '07 #12
JosAH
11,448 Expert 8TB
How a simple problem can become a complicated educational issue. If only the OP
would come back so this entire little thingy could be solved in a nice way.

kind regards,

Jos
Nov 18 '07 #13
How a simple problem can become a complicated educational issue. If only the OP
would come back so this entire little thingy could be solved in a nice way.

kind regards,

Jos
I don't understand anything about bits really.

Why can't you just post the solution, with annotations so i understand what it does? Then we'd all be happy.
Nov 18 '07 #14
JosAH
11,448 Expert 8TB
I don't understand anything about bits really.

Why can't you just post the solution, with annotations so i understand what it does? Then we'd all be happy.
No we won't:

1) you still won't understand;
2) if you'd turn our work in you'd be cheating;
3) we don't cooperate with cheating;
4) future coworkers won't like you as a cheater;
5) at the end you won't be happy either.

Read up on those simple bit shifting operators and don't try to cheat.

kind regards,

Jos
Nov 18 '07 #15
No we won't:

1) you still won't understand;
2) if you'd turn our work in you'd be cheating;
3) we don't cooperate with cheating;
4) future coworkers won't like you as a cheater;
5) at the end you won't be happy either.

Read up on those simple bit shifting operators and don't try to cheat.

kind regards,

Jos
I have read up on them, but they don't seem so simple. Maybe you could explain them, if you don't think i'll understand...
All you have to do is explain to me this bit shifting and bits in general. Then how i would use it for the concept of RGB.

But no.
Nov 18 '07 #16
oler1s
671 Expert 512MB
I have read up on them, but they don't seem so simple. Maybe you could explain them, if you don't think i'll understand...
But if you don't understand the material you find in your book or with Google, then you won't understand our explanations either. If there's a concept you don't understand, something about the material you don't get, you need to tell us what it is you don't understand.

All you have to do is explain to me this bit shifting and bits in general. Then how i would use it for the concept of RGB.
The concept can be explained with a Google search. You complain you don't understand the material. How do we know our explanation won't be wasted on you?

Moreoever, figuring out how to use bit shifting for RGB is the homework assignment. You need to think about it. As far as we can tell, you just want the answer, and you'll take the explanation if you feel like it.

I'll give you a short description of bits and bit shifting. Let's see if you go anywhere with this.

A bit can be thought of as an on/off switch, because that's effectively what it is in hardware. Let's call off 0 and on 1. These would be your binary digits. When it comes to numbers, bits can correspond perfectly with a binary number. Let's say you have two bits. You can have this.
Expand|Select|Wrap|Line Numbers
  1. Bit 1    Bit 0        Binary Number        Decimal Number
  2.     0        0                        00            0
  3.     0        1                        01            1
  4.     1        0                        10            2
  5.     1        1                        11            3
  6.  
Can you see a pattern with how many bits you need to have how large a number? R, G, and B go from 0 to 31. How many bits do they need minimum?

Now consider bit shift. As it sounds, it's simply a shift:
0010 << 1 becomes 0100
0001 << 3 becomes 1000
It's the reverse direction for >>.

Now think. Knowing how many bits there are in R, G, and B, and using bit shifting, how can you form a 16 bit "number" from R,G, and B?
Nov 18 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Muffinman | last post by:
Hi, I'm trying to build a script which will fade the colour of my text to another. Now I've got here a litte problem. Whene ever I want to replace one of the numbers in:...
2
by: Adam Teasdale Hartshorne | last post by:
I need to convert a colour defined as floats of R,G and B channels into the same R,G and B channels but defined by unsigned char's. Any help? Adam
4
by: Steven | last post by:
Can anyone direct me to a colour picker of the same (or similar quality) to the default .NET one but one that encompasses ability to specify colour by HEX value?
1
by: Anonieko Ramos | last post by:
Answer: http://weblogs.asp.net/tims/archive/2004/04/02/106310.aspx by Tim Sneath I've come across the situation on a number of occasions when coding where I've wanted to convert from a string...
4
by: Supra | last post by:
value of type "Integer" cannot be convert to system.color Public Sub APIHighlight2(ByVal BgColour As Integer, ByVal FgColour As Integer) SelectionHighlightBackColour(BgColour) Dim rtb As New...
1
by: tinab | last post by:
I have all my data in an excell sheet and I what to transfer it to ACAD. I need a function, macro that extracts the colour of a cell in excel and gives me back three RGB values in another cell. I...
5
by: JJ | last post by:
I have a function that returns an int that represents an rgb colour code (e.g. '5731173'). How can I convert this to a hex value of the form #D2D2D2 so that I can use it to color an object? ...
6
Robbie
by: Robbie | last post by:
Hi. I've made 2 functions which play around with colours. They convert a 'colour number' (I don't know what the proper name for it is, so I call it this - the Long given back by RGB(),...
5
by: beertje | last post by:
This has me a bit stumped... I'm trying to extract pictures from a file. So far I'm successfully retrieved the header and what I think is the colour for each pixel. Here's the description: ...
4
by: Arun | last post by:
I came across a message sometime back in 2002. Here's a link:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.