Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem in GDI+ LinearGradientBrush

Problem in GDI+ LinearGradientBrush

Scheduled Pinned Locked Moved C / C++ / MFC
graphicswinformscomlinuxhelp
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Naveen

    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 advance

    nave [OpenedFileFinder]

    M Offline
    M Offline
    Mark Salsbery
    wrote on last edited by
    #2

    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:

    N 1 Reply Last reply
    0
    • M Mark Salsbery

      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:

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #3

      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]

      M 1 Reply Last reply
      0
      • N Naveen

        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]

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #4

        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:

        N 1 Reply Last reply
        0
        • M Mark Salsbery

          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:

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #5

          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]

          M 1 Reply Last reply
          0
          • N Naveen

            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]

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #6

            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:

            N 1 Reply Last reply
            0
            • M Mark Salsbery

              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:

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #7

              Mark Salsbery wrote:

              You may have to come up with your own formula.

              :(( Thanks for your support.:rose:

              nave [OpenedFileFinder]

              M 1 Reply Last reply
              0
              • N Naveen

                Mark Salsbery wrote:

                You may have to come up with your own formula.

                :(( Thanks for your support.:rose:

                nave [OpenedFileFinder]

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #8

                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:

                N 1 Reply Last reply
                0
                • M Mark Salsbery

                  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:

                  N Offline
                  N Offline
                  Naveen
                  wrote on last edited by
                  #9

                  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]

                  M 1 Reply Last reply
                  0
                  • N Naveen

                    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]

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #10

                    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:

                    N 1 Reply Last reply
                    0
                    • M Mark Salsbery

                      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:

                      N Offline
                      N Offline
                      Naveen
                      wrote on last edited by
                      #11

                      Thanks

                      nave [OpenedFileFinder]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups