Click here to Skip to main content
15,897,187 members
Home / Discussions / Graphics
   

Graphics

 
AnswerRe: Scaling and Saving .... [modified] Pin
Mark Salsbery10-Apr-07 8:08
Mark Salsbery10-Apr-07 8:08 
QuestionHow to Install Photoshop PSD Previewer for Vista Pin
Roy4Magic9-Apr-07 8:14
Roy4Magic9-Apr-07 8:14 
AnswerRe: How to Install Photoshop PSD Previewer for Vista Pin
Raj Lal9-Apr-07 20:18
professionalRaj Lal9-Apr-07 20:18 
Questionvirtual makeover using C# Pin
samreengr87-Apr-07 11:07
samreengr87-Apr-07 11:07 
GeneralCreating Gradient Images Pin
Bradml7-Apr-07 0:32
Bradml7-Apr-07 0:32 
GeneralRe: Creating Gradient Images Pin
Rilhas19-May-07 11:09
Rilhas19-May-07 11:09 
GeneralRe: Creating Gradient Images Pin
Bradml19-May-07 11:54
Bradml19-May-07 11:54 
GeneralRe: Creating Gradient Images Pin
Rilhas20-May-07 2:12
Rilhas20-May-07 2:12 
Ok.

Modern computer graphics consist of pixels, which are the smallest units of color that can be represented on the screen. A screen of 1280x1024 contains 1.3 million pixels, and a modern display can set each one of those pixels to an individual color.

These pixels are divided into 3 components: the red, the green, and the blue. This is because almost every color that our eyes can distinguish can be obtained by combining diferent amounts of red, green, and blue. So, controlling only these 3 components can make it possible to obtain almost any possible color.

Then there is component resolution. Each component can be varied in intensity to achieve diferent colors, as I mentioned before. Usually we cannot distinguish between more than about 150 steps, meaning that if the span of a given component (from invisible (or off) to its full intensity) is divided into more than about 150 steps then we cannot tell the difference from one step to another. So we don't need to divide the span into more than 150 steps. However we divide it into 256 steps, to make it confortable to store components in computer memory (one byte per component) and for some other not so obvious reasons. This givs us a total of over 16 million individual colors (with 256 steps per component we get a total of 256x256x256 different colors).

To get white you sum all components (you set red, green, and blue, to full intensity). This is the whitest that a screen is able to represent. To get black you turn them all off, and that is the blackest that a screen can represent. Screens vary in their ability to make full white or full black. For example, my video projector is unable to make perfect white because the blue component is a little weaker than the others (as with many of the mid-range video projectors). Also, LCD's/TFT's are unable to make perfect black, usually because the material used for their construction usually reflects some envoronmental light.

If you want a gradient from black to white (from left to right, for example) then you should vary the component intensity from off (to the left) to on (to the right). Imagine an image of 800x600 and the following formula:

for(x=0; x<800; x++) {
for(y=0; y<600; y++) {
pixel(x,y).red=x/799*255;
pixel(x,y).green=x/799*255;
pixel(x,y).blue=x/799*255;
} // next y
} // next x

As you can see, x/799 is 1.0 only when x is 799, and that happens only at the rightmost pixel. Also, x/799 is zero on the left. An inversion would be simple, just replace x/799 by (799-x)/799. Also note that the component intensity does not depend on y, so there are changes only from left to right and not from top to bottom.

If you wanted full white on the top left and full white on the bottom right, the formula would be:

for(x=0; x<800; x++) {
for(y=0; y<600; y++) {
pixel(x,y).red=(x+y)/(799+599)*255;
pixel(x,y).green=(x+y)/(799+599)*255;
pixel(x,y).blue=(x+y)/(799+599)*255;
} // next y
} // next x

Again (x+y)/(799/599) is zero at the top left, and one at the bottom right. At the bottom left it is some intermediate level of gray (between black and white) as well as on the top right. If instead of black to white you wanted black to red then you just keep the green and blue at zero through all the operation.

for(x=0; x<800; x++) {
for(y=0; y<600; y++) {
pixel(x,y).red=(x+y)/(799+599)*255;
pixel(x,y).green=0;
pixel(x,y).blue=0;
} // next y
} // next x

At the top left all components are zero (black) and at the bottom right red is one and the others are zero. Orange is about full red, half gree, and zero blue. To go from black to orange you would do:

for(x=0; x<800; x++) {
for(y=0; y<600; y++) {
pixel(x,y).red=(x+y)/(799+599)*255;
pixel(x,y).green=(x+y)/(799+599)*128;
pixel(x,y).blue=0;
} // next y
} // next x

At the bottom right the pixel is full red, half green, and zero blue.

So, as you can see, the formula is simple after you know what you want. As you can imagine it would be easy to get gradients with many diferent colors by using more elaborate formulas. In general, you just need a function...

GetPixelComponets(float dx, float dy, int& dst_red, int& dst_green, int& dst_blue);

... that takes normalized X/Y coordinates (dx/dy from 0.0 to 1.0 so as not to depend on screen resolution) and computes what the component values dst_red, dst_green, and dst_blue should be for that screen point. Then that function simply has to take some set of references of the colors you want and where (in the above examples I wanted 2 reference points: a given color on the top left and another on the bottom right) and interpolate them (making smooth spatial transitions from a reference point to the next).

I hope this helps,
Rilhas
GeneralRe: Creating Gradient Images Pin
Bradml27-May-07 21:34
Bradml27-May-07 21:34 
QuestionHow to implement a teleprompter? Pin
beatles16925-Apr-07 0:32
beatles16925-Apr-07 0:32 
AnswerRe: How to implement a teleprompter? Pin
El Corazon6-Apr-07 7:03
El Corazon6-Apr-07 7:03 
QuestionNEED HELP REGARDING DIRECTX Pin
Faisal Khatri4-Apr-07 23:26
Faisal Khatri4-Apr-07 23:26 
AnswerRe: NEED HELP REGARDING DIRECTX Pin
El Corazon6-Apr-07 7:08
El Corazon6-Apr-07 7:08 
QuestionDrawing between desktop background and desktop icons? Pin
Soykaf3-Apr-07 5:55
Soykaf3-Apr-07 5:55 
QuestionBuffered Graphic Context Pin
Jim Warburton2-Apr-07 2:48
Jim Warburton2-Apr-07 2:48 
AnswerRe: Buffered Graphic Context Pin
Jim Warburton3-Apr-07 1:17
Jim Warburton3-Apr-07 1:17 
QuestionDirectShow Editing Services Pin
mjmim1-Apr-07 0:19
mjmim1-Apr-07 0:19 
QuestionVB.Net How to Break up an image Pin
The ANZAC30-Mar-07 18:39
The ANZAC30-Mar-07 18:39 
AnswerRe: VB.Net How to Break up an image Pin
Tall Dude1-Apr-07 20:38
Tall Dude1-Apr-07 20:38 
GeneralRe: VB.Net How to Break up an image Pin
The ANZAC1-Apr-07 21:57
The ANZAC1-Apr-07 21:57 
GeneralRe: VB.Net How to Break up an image Pin
Tall Dude2-Apr-07 8:15
Tall Dude2-Apr-07 8:15 
GeneralRe: VB.Net How to Break up an image Pin
Tall Dude2-Apr-07 13:51
Tall Dude2-Apr-07 13:51 
GeneralRe: VB.Net How to Break up an image Pin
Tall Dude3-Apr-07 9:22
Tall Dude3-Apr-07 9:22 
AnswerRe: VB.Net How to Break up an image Pin
Tall Dude3-Apr-07 14:33
Tall Dude3-Apr-07 14:33 
GeneralRe: VB.Net How to Break up an image Pin
The ANZAC3-Apr-07 18:37
The ANZAC3-Apr-07 18:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.