shadow.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "shadow.h"
00030 #include <qcolor.h>
00031
00032 ShadowEngine::ShadowEngine()
00033 {
00034 thickness_ = 1;
00035 multiplicationFactor_ = 10.0;
00036 }
00037
00038 ShadowEngine::~ShadowEngine()
00039 {
00040 }
00041
00042 QImage ShadowEngine::makeShadow(const QPixmap& textPixmap, const QColor &bgColor)
00043 {
00044 QImage result;
00045
00046
00047 int w = textPixmap.width();
00048 int h = textPixmap.height();
00049
00050
00051 int bgRed = bgColor.red();
00052 int bgGreen = bgColor.green();
00053 int bgBlue = bgColor.blue();
00054
00055 double alphaShadow;
00056
00057
00058
00059
00060 QImage img = textPixmap.convertToImage().convertDepth(32);
00061
00062
00063
00064
00065 if ((result.width() != w) || (result.height() != h))
00066 {
00067 result.create(w, h, 32);
00068 }
00069
00070 result.fill(0);
00071 result.setAlphaBuffer(true);
00072
00073 for (int i = thickness_; i < w - thickness_; i++)
00074 {
00075 for (int j = thickness_; j < h - thickness_; j++)
00076 {
00077 alphaShadow = decay(img, i, j);
00078 alphaShadow = (alphaShadow > 180.0) ? 180.0 : alphaShadow;
00079
00080 result.setPixel(i,j, qRgba(bgRed, bgGreen , bgBlue, (int) alphaShadow));
00081 }
00082 }
00083 return result;
00084 }
00085
00086 double ShadowEngine::decay(QImage& source, int i, int j)
00087 {
00088
00089 int w = source.width();
00090 int h = source.height();
00091 int sx, sy;
00092
00093 double alphaShadow = 0;
00094 double opacity = 0;
00095 for (int k = 1; k <= thickness_; k++) {
00096
00097
00098
00099
00100
00101 opacity = 0;
00102 for (int l = -k; l <= k; l++) {
00103 if (i < k)
00104 sx = 0;
00105 else if (i >= w - k)
00106 sx = w - 1;
00107 else
00108 sx = i + l;
00109
00110 for (int m = -k; m <= k; m++) {
00111 if (j < k)
00112 sy = 0;
00113 else if (j >= h - k)
00114 sy = h - 1;
00115 else
00116 sy = j + m;
00117
00118 opacity += qGray(source.pixel(sx, sy));
00119 }
00120 }
00121 alphaShadow += opacity / multiplicationFactor_;
00122 }
00123 return alphaShadow;
00124 }
This file is part of the documentation for kwin Library Version 3.4.1.