[Qt-interest] how to get color from QGraphicsRectItem which is filled with QLinearGradient

franki franki at franki.eu.org
Mon May 10 22:41:55 CEST 2010


Friday 07 of May 2010 19:59:36 Colin Kern napisał(a):
> On Fri, May 7, 2010 at 10:43 AM, franki <franki at franki.eu.org> wrote:
> > Question is: how to get color from specific point in this rectangle
> > filled with gradient?
>
> The best thing to do might be to just calculate the color yourself.
> It's a linear interpolation and you know all the information you'd
> need to do this.  You could peek into the Qt source code even and just
> find where they calculate the color.  I just spent a few minutes
> looking for this, but it didn't jump out at me.  I think it's going to
> be somewhere in qbrush.cpp.
>
> I think it might be useful to have a function in QGradient that takes
> a QPoint and returns the QColor for that point.  It seems like this
> would have to be done internally already to do the painting, so it
> shouldn't take too much to expose it in the API.

Thanks for answer, and sorry for delay in sending this email

About calculating the color from gradient, when this gradient has 3 points 
with different colors, like this one below, I don't see how could I calculate 
color...
gradient.setColorAt(0, QColor::fromRgb(255, 98, 0, 255));
gradient.setColorAt(0.4, QColor::fromRgb(255, 249, 0, 255));
gradient.setColorAt(1, QColor::fromRgb(64, 0, 255, 255));
(from red, to yellow, to blue)

This function from QGradient it would be convinient, but I've searched for it 
and I haven't found it, so You suggest that it is somewhere, but not 
documented? 

Anyway, I came up with idea of having QImage object in memory, so I will fill 
this object with same gradient as QGraphicsRectItem (which is shown as 
temperature bar). They will have the same size (in pixels) so from this 
QImage object I will take color with QRgb QImage::pixel function.
Should that work?
The advantage is that when I will be playing around with colors of this 
gradient I won't have to worry about calculating color, because this QImage 
object will be always painted same as temperature bar shown on scene.
 
regards
Marek

>
> Colin






More information about the Qt-interest-old mailing list