-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearGradientShader.cpp
More file actions
90 lines (70 loc) · 2.58 KB
/
Copy pathLinearGradientShader.cpp
File metadata and controls
90 lines (70 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "GShader.h"
#include "GBitmap.h"
#include "GBlendModes.h"
#include "GMatrix.h"
#include "GPixel.h"
#include "GPoint.h"
class MyLinearGradientShader: public GShader {
public:
MyLinearGradientShader(GPoint p0, GPoint p1, const GColor colors[], int count, TileMode tile) {
this->fColors = (GColor*) malloc(count * sizeof(GColor));
memcpy(this->fColors, colors, count * sizeof(GColor));
this->colorCount = count;
this->fTile = tile;
if(p0.fX > p1.fX) {
std::swap(p0, p1);
}
float dx = p1.fX - p0.fX, dy = p1.fY - p0.fY;
fUnitMatrix = GMatrix(
dx, -dy, p0.fX,
dy, dx, p0.fY);
}
bool isOpaque() override {
return false;
}
bool setContext(const GMatrix& ctm) override {
fInverse = fInverse.Concat(ctm, fUnitMatrix);
return fInverse.invert(&fInverse);
}
void shadeRow(int x, int y, int count, GPixel row[]) override {
GPoint pts[1]{ x + 0.5f, y + 0.5f };
fInverse.mapPoints(pts, pts, 1);
GPoint start = pts[0];
float a = fInverse[0];
float ft;
for (int i = 0; i < count; i++) {
ft = start.fX;
if (fTile == TileMode::kRepeat) {
ft = ft - GFloorToInt(ft);
} else if (fTile == TileMode::kMirror) {
ft *= 0.5;
ft = ft - GFloorToInt(ft);
//
ft *= 2;
} else {
ft = std::max(0.0f, std::min(1.0f, ft));
}
ft = ft * (colorCount - 1);
int idx = GFloorToInt(ft);
float loc = ft - idx;
GColor c1 = fColors[idx].pinToUnit(), c2 = fColors[idx + 1].pinToUnit();
row[i] = convertColorToPixel(GColor::MakeARGB(c1.fA * (1 - loc) + c2.fA * loc,
c1.fR * (1 - loc) + c2.fR * loc,
c1.fG * (1 - loc) + c2.fG * loc,
c1.fB * (1 - loc) + c2.fB * loc));
start.fX += a;
}
}
private:
GColor* fColors;
GMatrix fInverse;
GMatrix fUnitMatrix;
int colorCount;
TileMode fTile;
};
std::unique_ptr<GShader> GCreateLinearGradient(GPoint p0, GPoint p1, const GColor colors[], int count, GShader::TileMode tile) {
if(count < 1) {
return nullptr;
}
return std::unique_ptr<GShader>(new MyLinearGradientShader(p0, p1, colors, count, tile));
}