Newer
Older
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef ICONGENERATOR_H
#define ICONGENERATOR_H
#include <QList>
#include <QtDebug>
#include "../shapes/shapecategory.h"
#include "../shapes/shape.h"
#include "../shapes/shapedefinitions.h"
#include "colortheme.h"
#include "rectangle.h"
namespace rendering {
class IconGenerator
{
private:
static QList<QPoint> shapeOne_;
static QList<QPoint> shapeTwo_;
static QList<QPoint> shapeThree_;
static QList<QPoint> shapeFour_;
static QList<void(*)(rendering::Renderer&, int, int)> outerShapes_;
static QList<void(*)(rendering::Renderer&, int, int)> centerShapes_;
static QList<shapes::ShapeCategory> defaultCategories_;
static bool isDuplicate(QList<int> &source, int newValue, QList<int> &duplicateValues)
{
if (duplicateValues.contains(newValue))
{
for (int i = 0; i < duplicateValues.size(); i++)
{
if (source.contains(duplicateValues[i]))
{
return true;
}
}
}
return false;
}
protected:
QList<shapes::ShapeCategory> getCategories();
virtual QList<shapes::Shape> getShapes(ColorTheme &theme, QByteArray &hash);
virtual void RenderBackground(Renderer &renderer, Rectangle rect, IdenticonStyle &style, ColorTheme &colorTheme, QByteArray &hash)
{
Q_UNUSED(rect);
Q_UNUSED(style);
Q_UNUSED(colorTheme);
Q_UNUSED(hash);
QColor backCol = style.backCol();
renderer.setBackgroundColor(backCol);
}
virtual Rectangle normalizeRectangle(Rectangle & rect)
{
auto size = qMin(rect.width(), rect.height());
// Make size a multiple of the cell count
size -= size % cellCount();
return Rectangle(
rect.x() + (rect.width() - size) / 2,
rect.y() + (rect.height() - size) / 2,
size,
size
);
}
virtual void RenderForeground(Renderer &renderer, Rectangle &rect, IdenticonStyle &style, ColorTheme &colorTheme, QByteArray &hash)
{
Q_UNUSED(style);
// Ensure rect is quadratic and a multiple of the cell count
auto normalizedRect = normalizeRectangle(rect);
qDebug() << "cellCount" << cellCount();
qDebug() << "width" << normalizedRect.width();
auto cellSize = normalizedRect.width() / cellCount();
auto shapes = getShapes(colorTheme, hash);
for (auto shape : shapes)
{
auto rotation = shape.getStartRotationIndex();
QColor shapeColor = shape.getShapeColor();
renderer.beginShape(shapeColor);
for (int i = 0; i < shape.getPositions().size(); i++)
{
auto position = shape.getPositions()[i];
Transform transform(
normalizedRect.x() + position.x() * cellSize,
normalizedRect.y() + position.y() * cellSize,
cellSize, rotation++ % 4);
renderer.setTransform(transform);
shape.getDefinition()(renderer, cellSize, i);
}
//renderer.endShape();
}
}
static int
hashQString(const QString &input)
{
auto hash = 0;
for (int i = 0; i < input.length(); i++) {
hash = input.at(i).digitValue() + ((hash << 5) - hash);
}
return hash;
}
static qreal getHue(QByteArray &hash)
{
// Create a color for the input
auto hashInt = hashQString(hash);
// create a hue value based on the hash of the input.
auto userHue = qAbs(hashInt % 360);
return userHue / 360.0;
}
static char getOctet(QByteArray &arr, const int index)
{
auto byteIndex = index / 2;
auto byteValue = arr.at(byteIndex);
if (byteIndex * 2 == index)
{
byteValue = byteValue >> 4;
}
else
{
byteValue = byteValue & 0xf;
}
return byteValue;
}
public:
IconGenerator();
virtual int cellCount() {
return 4;
}
void generate(Renderer &renderer, Rectangle &rect, IdenticonStyle &style, QByteArray &hash);
virtual ~IconGenerator() = default;
};
} // namespace rendering
#endif // ICONGENERATOR_H