Problem in GDI+ LinearGradientBrush
-
Hi all, I am trying to draw a gradient rectangle using the below code. But the problem is am not getting the correct color in the bottom. As per the code the expected color is RGB(0,0,255). But now I am getting RGB(3,0,252). Is there any way to show the correct value?
Color clr1( 255,255,0,0 ); Color clr2( 255,0,0,255 ); Rect rect( 0,0,100,100 ); LinearGradientBrush pushedFace( rect, clr1, clr2, LinearGradientModeVertical ); CClientDC dc(this); Graphics gr(dc.m_hDC); gr.FillRectangle( &pushedFace,rect );
Thanks in advancenave [OpenedFileFinder]
-
Hi all, I am trying to draw a gradient rectangle using the below code. But the problem is am not getting the correct color in the bottom. As per the code the expected color is RGB(0,0,255). But now I am getting RGB(3,0,252). Is there any way to show the correct value?
Color clr1( 255,255,0,0 ); Color clr2( 255,0,0,255 ); Rect rect( 0,0,100,100 ); LinearGradientBrush pushedFace( rect, clr1, clr2, LinearGradientModeVertical ); CClientDC dc(this); Graphics gr(dc.m_hDC); gr.FillRectangle( &pushedFace,rect );
Thanks in advancenave [OpenedFileFinder]
Naveen.R wrote:
Is there any way to show the correct value?
Add 1 to the rect height. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Naveen.R wrote:
Is there any way to show the correct value?
Add 1 to the rect height. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
you mean like
LinearGradientBrush pushedFace( **Rect( 0,0, 30, 101 )**, clr1, clr2, LinearGradientModeVertical );
. In this case the color became more lighter. RGB(5,0,250) :(nave [OpenedFileFinder]
-
you mean like
LinearGradientBrush pushedFace( **Rect( 0,0, 30, 101 )**, clr1, clr2, LinearGradientModeVertical );
. In this case the color became more lighter. RGB(5,0,250) :(nave [OpenedFileFinder]
You're right! LOL sorry. The problem is with the math (apparently which I'm too tired to comprehend :)). Incrementing evenly through 100 rows from 0 to 255, the last row ends at 252.45 or 252. There's no way around it that I know of except adjusting the width/height to a value that makes it come out exact. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
You're right! LOL sorry. The problem is with the math (apparently which I'm too tired to comprehend :)). Incrementing evenly through 100 rows from 0 to 255, the last row ends at 252.45 or 252. There's no way around it that I know of except adjusting the width/height to a value that makes it come out exact. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark Salsbery wrote:
Incrementing evenly through 100 rows from 0 to 255
Well I tried making the following changes. Set the height as 255.clr1 = RGB( 0,0,0), clr2 = RGB(0,0,255 ) In this case the color should come correct isnt it? but now the color is 0,0,254 at the bottom( I know the color change is not noticeable. But I have to meet the specifications... ).
nave [OpenedFileFinder]
-
Mark Salsbery wrote:
Incrementing evenly through 100 rows from 0 to 255
Well I tried making the following changes. Set the height as 255.clr1 = RGB( 0,0,0), clr2 = RGB(0,0,255 ) In this case the color should come correct isnt it? but now the color is 0,0,254 at the bottom( I know the color change is not noticeable. But I have to meet the specifications... ).
nave [OpenedFileFinder]
You may have to come up with your own formula. If they use a formula similar to this one: Using color gradients as backgrounds in your dialogs and views[^], then it will never be exact on the last row. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
You may have to come up with your own formula. If they use a formula similar to this one: Using color gradients as backgrounds in your dialogs and views[^], then it will never be exact on the last row. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark Salsbery wrote:
You may have to come up with your own formula.
:(( Thanks for your support.:rose:
nave [OpenedFileFinder]
-
Mark Salsbery wrote:
You may have to come up with your own formula.
:(( Thanks for your support.:rose:
nave [OpenedFileFinder]
I'll look deeper into this in the morning - for now, I have to sleep :zzz: Good luck! Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I'll look deeper into this in the morning - for now, I have to sleep :zzz: Good luck! Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark Salsbery wrote:
I'll look deeper into this in the morning - for now, I have to sleep
Thanks for that... Good night.
nave [OpenedFileFinder]
-
Mark Salsbery wrote:
I'll look deeper into this in the morning - for now, I have to sleep
Thanks for that... Good night.
nave [OpenedFileFinder]
OK, if you want to roll your own gradient rect, this formula should work (((r2 - r1) / (rectheight - 1)) * currentrow) + r1 = rdest (((g2 - g1) / (rectheight - 1)) * currentrow) + g1 = gdest (((b2 - b1) / (rectheight - 1)) * currentrow) + b1 = bdest where r,g,b == red,green,blue components Desired Start row color == RGB(r1,g1,b1) Desired End row color == RGB(r2,g2,b2) currentrow == current row of rect being drawn (0-based) RGB(rdest,gdest,bdest) is the resulting color for the currentrow Note this requires floating point so it will be slower than the standard integer formula. The example is for vertical gradient - it would work for columns as well (horizontal gradient). Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
OK, if you want to roll your own gradient rect, this formula should work (((r2 - r1) / (rectheight - 1)) * currentrow) + r1 = rdest (((g2 - g1) / (rectheight - 1)) * currentrow) + g1 = gdest (((b2 - b1) / (rectheight - 1)) * currentrow) + b1 = bdest where r,g,b == red,green,blue components Desired Start row color == RGB(r1,g1,b1) Desired End row color == RGB(r2,g2,b2) currentrow == current row of rect being drawn (0-based) RGB(rdest,gdest,bdest) is the resulting color for the currentrow Note this requires floating point so it will be slower than the standard integer formula. The example is for vertical gradient - it would work for columns as well (horizontal gradient). Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks
nave [OpenedFileFinder]