can Qt darker / lighter a image ?
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
Re: can Qt darker / lighter a image ?
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.
Re: can Qt darker / lighter a image ?
hi
try this code
Code:
p2.setY(tmpImage->height());
gradient.setColorAt(0, Qt::transparent);
gradient.
setColorAt(1,
QColor(0,
0,
0,
255));
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 :)
Re: can Qt darker / lighter a image ?
Hello, thanks , follow your suggestion, I have below code. and better idea ?
place here for someone may also looking for ...
Code:
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;
}
}
}
}