PDA

View Full Version : can Qt darker / lighter a image ?



wesley
25th January 2010, 03:58
I want to implment a widget to indicate the light status, and I alread have a light image.

Does Qt has ability to darker/lighter a image ? how to do ?

and the image has some transparent area, and alpha channel, is it possible that the result image still have transparent and alpha ?
sorry, i am not familiar with this ...

Tks ...

/WX

aamer4yu
25th January 2010, 04:20
From the QImage you can get pixel ( QImage::pixel ) . You can then set the pixel to desired color.QColor::setRgb and use QColor::darker or QColor::lighter .

Other wise you can also have look at QGraphicsEffect and derived classes.

vishwajeet.dusane
25th January 2010, 06:02
hi

try this code



QImage *tmpImage = new QImage(SOME_IMAGE_PATH);
QPoint p1, p2;
p2.setY(tmpImage->height());

QLinearGradient gradient(p1, p2);
gradient.setColorAt(0, Qt::transparent);
gradient.setColorAt(1, QColor(0, 0, 0, 255));

QPainter p(tmpImage);
p.fillRect(0, 0, tmpImage->width(), tmpImage->height(), gradient);

gradient.setColorAt(0,QColor(0, 0, 0, 255));
gradient.setColorAt(1, Qt::transparent);
p.fillRect(0,0, tmpImage->width(), tmpImage->height(), gradient);

p.end();


You can use this code to darker or lighter the image, depending upon ur requirement change gradient color range.

BTW this is gradient color i have used on the image. you can use plain color as well :)

wesley
27th January 2010, 07:56
Hello, thanks , follow your suggestion, I have below code. and better idea ?

place here for someone may also looking for ...



void do_light_adjust(QImage *image, int factor)
{
if (image == NULL || image->isNull() || factor == 0)
return;

int bytes_per_pixel = image->bytesPerLine() / image->width();
uchar *pixel = NULL;
QRgb *rgba;

if (factor > 0) { // lighter
factor += 100;
for (int h = 0; h < image->height(); h++) {
pixel = image->scanLine(h);
for (int w = 0; w < image->width(); w++) {
rgba = (QRgb *)pixel;
if (qAlpha(*rgba) != 0 && (qRed(*rgba) != 0 || qGreen(*rgba) != 0 || qBlue(*rgba) != 0))
*rgba = QColor::fromRgba(*rgba).lighter(factor).rgba();
pixel += bytes_per_pixel;
}
}
} else { // darker
factor = -factor;
factor += 100;
for (int h = 0; h < image->height(); h++) {
uchar *pixel = image->scanLine(h);
for (int w = 0; w < image->width(); w++) {
rgba = (QRgb *)pixel;
if (qAlpha(*rgba) != 0 && (qRed(*rgba) != 0 || qGreen(*rgba) != 0 || qBlue(*rgba) != 0))
*rgba = QColor::fromRgba(*rgba).darker(factor).rgba();
pixel += bytes_per_pixel;
}
}
}
}