Home > Net >  Can I construct a color gradient in a rectangular region with given colors at the cornes in Qt?
Can I construct a color gradient in a rectangular region with given colors at the cornes in Qt?

Time:04-15

I have a matrix of values of colours evenly spread across a grid with some pixels in between them. Something like this:

r _ _ g _ _ r ......
_ _ _ _ _ _ _ .....
_ _ _ _ _ _ _ ....
b _ _ r _ _ g ....
_ _ _ _ _ _ _ ....
_ _ _ _ _ _ _ ....
g _ _ b _ _ r ....
. . . . . . . ....
. . . . . . . ....
. . . . . . . .....

How can I construct/draw/paint a gradient with smooth transition in between colours in Qt C ? What tools should I use to achieve this?

I feel that somehow if I interpolate the colour values in the following rectangle, I can do it for the whole grid. -

   r
  _ _ 
g _ _ r
  _ _
   b

But the classes QLinearGradient, QRadialGradient, and QConicGradient seem to be of no use in this scenario as they take only two points as arguments.

CodePudding user response:

You can use bilinear interpolation to calculate colors between points in each cell:

for any given point h find colors e and f
where r, g, and b chanel of e is equal to   
r(e) = r(a)   (r(b) - r(a)) * distance(a,e) / distance(a,b)
g(e) = ...
b(e) = ...

r(f) = r(c)   (r(d) - r(c)) * distance(c,f) / distance(c,d)
g(f) = ...
b(f) = ...

and then find r(h),g(h) and b(h) interpolating between e and f vertically

r(h) = r(e)   (r(f) - r(e)) * distance(e,h) / distance(e,f)
g(h) = ...
b(h) = ...

a-e---b
| h   |
| |   |
| |   |
c-f---d
  • Related