Skip to content
Snippets Groups Projects
Commit 28617287 authored by Joe Donofry's avatar Joe Donofry
Browse files

Merge branch 'code-cleanup' into 'master'

Code cleanup

See merge request !2
parents b4196b51 63cc4b45
No related branches found
No related tags found
1 merge request!2Code cleanup
...@@ -47,7 +47,6 @@ HEADERS += src/identicon.h \ ...@@ -47,7 +47,6 @@ HEADERS += src/identicon.h \
src/jdenticonplugin.h \ src/jdenticonplugin.h \
includes/jdenticoninterface.h includes/jdenticoninterface.h
SOURCES += src/identicon.cpp \ SOURCES += src/identicon.cpp \
src/identiconstyle.cpp \
src/qtidenticon.cpp \ src/qtidenticon.cpp \
src/rendering/colortheme.cpp \ src/rendering/colortheme.cpp \
src/rendering/colorutils.cpp \ src/rendering/colorutils.cpp \
......
#include "identicon.h" #include "identicon.h"
IdenticonStyle Identicon::defaultStyle_ = {}; Identicon::Identicon(const QString &hash, int size)
Identicon::Identicon(QString &hash, int size) : hash_(hash)
{ , size_(size)
size_ = size; {}
hash_ = hash;
}
Identicon
Identicon::fromHash(QByteArray &hash, int size)
{
QString str = QString(hash);
Identicon fromHash(str, size);
return fromHash;
}
QString QString
Identicon::generateSvg(Identicon &icon, bool fragment) Identicon::generateSvg(Identicon &icon, bool fragment)
...@@ -28,7 +18,7 @@ rendering::Rectangle ...@@ -28,7 +18,7 @@ rendering::Rectangle
Identicon::getIconBounds() Identicon::getIconBounds()
{ {
// Round to nearest integer // Round to nearest integer
auto padding = qFloor(defaultStyle_.padding() * size() + 0.5); auto padding = qFloor(IdenticonStyle::padding() * size() + 0.5);
return rendering::Rectangle(padding, padding, size() - padding * 2, size() - padding * 2); return rendering::Rectangle(padding, padding, size() - padding * 2, size() - padding * 2);
} }
...@@ -36,5 +26,5 @@ Identicon::getIconBounds() ...@@ -36,5 +26,5 @@ Identicon::getIconBounds()
void void
Identicon::draw(rendering::Renderer &renderer, rendering::Rectangle &rect) Identicon::draw(rendering::Renderer &renderer, rendering::Rectangle &rect)
{ {
iconGenerator_.generate(renderer, rect, defaultStyle_, hash_); iconGenerator_.generate(renderer, rect, hash_);
} }
...@@ -15,14 +15,11 @@ private: ...@@ -15,14 +15,11 @@ private:
int size_; int size_;
rendering::IconGenerator iconGenerator_; rendering::IconGenerator iconGenerator_;
IdenticonStyle style_; IdenticonStyle style_;
static IdenticonStyle defaultStyle_;
public: public:
Identicon(QString &hash, int size); Identicon(const QString &hash, int size);
int size() { return size_; } int size() { return size_; }
rendering::Rectangle getIconBounds(); rendering::Rectangle getIconBounds();
static Identicon fromHash(QByteArray &hash, int size);
static Identicon fromHash(QString &hash, int size);
static QString generateSvg(Identicon &identicon, bool fragment); static QString generateSvg(Identicon &identicon, bool fragment);
void draw(rendering::Renderer &renderer, rendering::Rectangle &rect); void draw(rendering::Renderer &renderer, rendering::Rectangle &rect);
}; };
......
#include <stdexcept>
#include "identiconstyle.h"
#include <stdexcept>
IdenticonStyle::IdenticonStyle() {}
IdenticonStyle::IdenticonStyle(IdenticonStyle &other)
{
hues_ = other.hues();
backColor_ = other.backCol();
padding_ = other.padding();
colorSaturation_ = other.colorSaturation();
grayscaleSaturation_ = other.grayscaleSaturation();
minColorLightness_ = other.minColorLightness();
maxColorLightness_ = other.maxColorLightness();
minGrayLightness_ = other.minGrayLightness();
maxGrayLightness_ = other.maxGrayLightness();
}
void
IdenticonStyle::setHues(QList<qreal> &hues)
{
hues_ = hues;
}
void
IdenticonStyle::setBackCol(QColor &color)
{
backColor_ = color;
}
void
IdenticonStyle::setPadding(qreal padding)
{
if (padding < 0.0 || padding > 0.4) {
throw new std::out_of_range("Only padding values in the range [0.0, 0.4] are valid.");
}
padding_ = padding;
}
void
IdenticonStyle::setColorSaturation(qreal saturation)
{
if (saturation < 0 || saturation > 1) {
throw new std::out_of_range("Only saturation values in the range [0.0, 1.0] are allowed.");
}
colorSaturation_ = saturation;
}
void
IdenticonStyle::setGrayscaleSaturation(qreal saturation)
{
if (saturation < 0 || saturation > 1) {
throw new std::out_of_range("Only saturation values in the range [0.0, 1.0] are allowed.");
}
grayscaleSaturation_ = saturation;
}
void
IdenticonStyle::setMinColorLightness(qreal lightness)
{
if (lightness < 0.0) {
throw new std::out_of_range("Only lightness values between [0.0, 1.0] are allowed.");
}
minColorLightness_ = lightness;
}
void
IdenticonStyle::setMaxColorLightness(qreal lightness)
{
if (lightness > 1.0) {
throw new std::out_of_range("Only lightness values between [0.0, 1.0] are allowed.");
}
maxColorLightness_ = lightness;
}
void
IdenticonStyle::setMinGrayLightness(qreal lightness)
{
if (lightness < 0.0) {
throw new std::out_of_range("Only lightness values between [0.0, 1.0] are allowed.");
}
minGrayLightness_ = lightness;
}
void
IdenticonStyle::setMaxGrayLightness(qreal lightness)
{
if (lightness > 1.0) {
throw new std::out_of_range("Only lightness values between [0.0, 1.0] are allowed.");
}
maxGrayLightness_ = lightness;
}
...@@ -2,42 +2,18 @@ ...@@ -2,42 +2,18 @@
#define IDENTICONSTYLE_H #define IDENTICONSTYLE_H
#include <QColor> #include <QColor>
#include <QList>
class IdenticonStyle class IdenticonStyle
{ {
private:
QList<qreal> hues_;
QColor backColor_ = QColor(0, 0, 0, 0);
qreal padding_ = 0.08;
qreal colorSaturation_ = 0.5;
qreal grayscaleSaturation_ = 0.0;
qreal minColorLightness_ = 0.4;
qreal maxColorLightness_ = 0.8;
qreal minGrayLightness_ = 0.3;
qreal maxGrayLightness_ = 0.9;
public: public:
IdenticonStyle(); static QColor backCol() { return QColor(0, 0, 0, 0); }
IdenticonStyle(IdenticonStyle &other); static qreal padding() { return 0.08; }
QList<qreal> hues() { return hues_; } static qreal colorSaturation() { return 0.5; }
QColor backCol() { return backColor_; } static qreal grayscaleSaturation() { return 0.0; }
qreal padding() { return padding_; } static qreal minColorLightness() { return 0.4; }
qreal colorSaturation() { return colorSaturation_; } static qreal maxColorLightness() { return 0.8; }
qreal grayscaleSaturation() { return grayscaleSaturation_; } static qreal minGrayLightness() { return 0.3; }
qreal minColorLightness() { return minColorLightness_; } static qreal maxGrayLightness() { return 0.9; }
qreal maxColorLightness() { return maxColorLightness_; }
qreal minGrayLightness() { return minGrayLightness_; }
qreal maxGrayLightness() { return maxGrayLightness_; }
void setHues(QList<qreal> &hues);
void setBackCol(QColor &color);
void setPadding(qreal padding);
void setColorSaturation(qreal saturation);
void setGrayscaleSaturation(qreal saturation);
void setMinColorLightness(qreal lightness);
void setMaxColorLightness(qreal lightness);
void setMinGrayLightness(qreal lightness);
void setMaxGrayLightness(qreal lightness);
}; };
#endif // IDENTICONSTYLE_H #endif // IDENTICONSTYLE_H
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
QString QString
JdenticonPlugin::generate(const QString &message, uint16_t size) JdenticonPlugin::generate(const QString &message, uint16_t size)
{ {
//QByteArray byteArr = QByteArray::fromStdString(message.toStdString()); Identicon identicon(message, size);
QString copy = message;
Identicon identicon(copy, size);
return identicon.generateSvg(identicon, false); return identicon.generateSvg(identicon, false);
} }
#include <stdexcept>
#include "colortheme.h" #include "colortheme.h"
#include <stdexcept> #include <stdexcept>
#include "../identiconstyle.h"
namespace rendering { namespace rendering {
ColorTheme::ColorTheme(qreal hue, IdenticonStyle &style) ColorTheme::ColorTheme(qreal hue)
{ {
if (style.hues().size() > 0) { darkGray_ = ColorUtils::fromHslCompensated(
// Multiply with 0.999 to change the range to [0, 1) hue, IdenticonStyle::grayscaleSaturation(), IdenticonStyle::minGrayLightness());
int hueIndex = qFloor(0.999 * hue * style.hues().size());
hue = style.hues().at(hueIndex);
}
darkGray_ =
ColorUtils::fromHslCompensated(hue, style.grayscaleSaturation(), style.minGrayLightness());
midColor_ = ColorUtils::fromHslCompensated( midColor_ = ColorUtils::fromHslCompensated(
hue, style.colorSaturation(), (style.minColorLightness() + style.maxColorLightness()) / 2); hue,
lightGray_ = IdenticonStyle::colorSaturation(),
ColorUtils::fromHslCompensated(hue, style.grayscaleSaturation(), style.maxGrayLightness()); (IdenticonStyle::minColorLightness() + IdenticonStyle::maxColorLightness()) / 2);
lightColor_ = lightGray_ = ColorUtils::fromHslCompensated(
ColorUtils::fromHslCompensated(hue, style.colorSaturation(), style.maxColorLightness()); hue, IdenticonStyle::grayscaleSaturation(), IdenticonStyle::maxGrayLightness());
darkColor_ = lightColor_ = ColorUtils::fromHslCompensated(
ColorUtils::fromHslCompensated(hue, style.colorSaturation(), style.minColorLightness()); hue, IdenticonStyle::colorSaturation(), IdenticonStyle::maxColorLightness());
darkColor_ = ColorUtils::fromHslCompensated(
hue, IdenticonStyle::colorSaturation(), IdenticonStyle::minColorLightness());
} }
QColor & QColor
ColorTheme::operator[](int index) ColorTheme::operator[](int index) const
{ {
if (index == 0) if (index == 0)
return darkGray_; return darkGray_;
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include <QColor> #include <QColor>
#include "../identiconstyle.h"
#include "colorutils.h" #include "colorutils.h"
namespace rendering { namespace rendering {
...@@ -19,9 +18,9 @@ private: ...@@ -19,9 +18,9 @@ private:
QColor lightColor_; QColor lightColor_;
public: public:
ColorTheme(qreal hue, IdenticonStyle &style); ColorTheme(qreal hue);
int count() { return 5; } int count() const { return 5; }
QColor &operator[](int index); QColor operator[](int index) const;
}; };
} // namespace rendering } // namespace rendering
......
#include <stdexcept>
#include "colorutils.h" #include "colorutils.h"
#include <stdexcept> #include <stdexcept>
......
...@@ -3,22 +3,22 @@ ...@@ -3,22 +3,22 @@
#include <QCryptographicHash> #include <QCryptographicHash>
namespace rendering { namespace rendering {
QList<QPoint> IconGenerator::shapeOne_({QPoint(1, 0), static QList<QPoint> shapeOne_({QPoint(1, 0),
QPoint(2, 0), QPoint(2, 0),
QPoint(2, 3), QPoint(2, 3),
QPoint(1, 3), QPoint(1, 3),
QPoint(0, 1), QPoint(0, 1),
QPoint(3, 1), QPoint(3, 1),
QPoint(3, 2), QPoint(3, 2),
QPoint(0, 2)}); QPoint(0, 2)});
QList<QPoint> IconGenerator::shapeTwo_({{0, 0}, {3, 0}, {3, 3}, {0, 3}}); static QList<QPoint> shapeTwo_({{0, 0}, {3, 0}, {3, 3}, {0, 3}});
QList<QPoint> IconGenerator::shapeThree_({{1, 1}, {2, 1}, {2, 2}, {1, 2}}); static QList<QPoint> shapeThree_({{1, 1}, {2, 1}, {2, 2}, {1, 2}});
QList<void (*)(rendering::Renderer &, int, int)> IconGenerator::centerShapes_ = static QList<void (*)(rendering::Renderer &, int, int)> centerShapes_ =
shapes::ShapeDefinitions::CenterShapes(); shapes::ShapeDefinitions::CenterShapes();
QList<void (*)(rendering::Renderer &, int, int)> IconGenerator::outerShapes_ = static QList<void (*)(rendering::Renderer &, int, int)> outerShapes_ =
shapes::ShapeDefinitions::OuterShapes(); shapes::ShapeDefinitions::OuterShapes();
QList<shapes::ShapeCategory> IconGenerator::defaultCategories_ = { static QList<shapes::ShapeCategory> defaultCategories_ = {
// Sides // Sides
shapes::ShapeCategory(8, 2, 3, shapeOne_, outerShapes_), shapes::ShapeCategory(8, 2, 3, shapeOne_, outerShapes_),
// Corner // Corner
...@@ -35,7 +35,7 @@ IconGenerator::getCategories() ...@@ -35,7 +35,7 @@ IconGenerator::getCategories()
} }
QList<shapes::Shape> QList<shapes::Shape>
IconGenerator::getShapes(ColorTheme &theme, QString &hash) IconGenerator::getShapes(const ColorTheme &theme, const QByteArray &hash)
{ {
QList<shapes::Shape> shapes; QList<shapes::Shape> shapes;
QList<int> usedColorThemeIndexes; QList<int> usedColorThemeIndexes;
...@@ -73,24 +73,16 @@ IconGenerator::getShapes(ColorTheme &theme, QString &hash) ...@@ -73,24 +73,16 @@ IconGenerator::getShapes(ColorTheme &theme, QString &hash)
} }
void void
IconGenerator::generate(Renderer &renderer, Rectangle &rect, IdenticonStyle &style, QString &input) IconGenerator::generate(Renderer &renderer, Rectangle &rect, QString &input)
{ {
auto hue = getHue(input); QByteArray hash = QCryptographicHash::hash(input.toUtf8(), QCryptographicHash::Sha1);
auto hue = getHue(hash);
qDebug() << "hue" << hue; qDebug() << "hue" << hue;
auto colorTheme = ColorTheme(hue, style); auto colorTheme = ColorTheme(hue);
QString hash =
QString(QCryptographicHash::hash(input.toUtf8(), QCryptographicHash::Sha1).toHex());
RenderBackground(renderer, rect, style, colorTheme, hash); RenderBackground(renderer);
RenderForeground(renderer, rect, style, colorTheme, hash); RenderForeground(renderer, rect, colorTheme, hash);
} }
uint32_t
IconGenerator::hashQString(const QString &input)
{
auto h = QCryptographicHash::hash(input.toUtf8(), QCryptographicHash::Sha1);
return (h[0] << 24) ^ (h[1] << 16) ^ (h[2] << 8) ^ h[3];
}
} // namespace rendering } // namespace rendering
...@@ -13,18 +13,9 @@ ...@@ -13,18 +13,9 @@
namespace rendering { namespace rendering {
class IconGenerator class IconGenerator final
{ {
private: 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) static bool isDuplicate(QList<int> &source, int newValue, QList<int> &duplicateValues)
{ {
if (duplicateValues.contains(newValue)) { if (duplicateValues.contains(newValue)) {
...@@ -39,21 +30,9 @@ private: ...@@ -39,21 +30,9 @@ private:
protected: protected:
QList<shapes::ShapeCategory> getCategories(); QList<shapes::ShapeCategory> getCategories();
virtual QList<shapes::Shape> getShapes(ColorTheme &theme, QString &hash); QList<shapes::Shape> getShapes(const ColorTheme &theme, const QByteArray &hash);
virtual void RenderBackground(Renderer &renderer, void RenderBackground(Renderer &renderer) { renderer.setBackgroundColor(QColor(0, 0, 0, 0)); }
Rectangle rect, Rectangle normalizeRectangle(Rectangle &rect)
IdenticonStyle &style,
ColorTheme &colorTheme,
QString &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()); auto size = qMin(rect.width(), rect.height());
...@@ -63,13 +42,11 @@ protected: ...@@ -63,13 +42,11 @@ protected:
return Rectangle( return Rectangle(
rect.x() + (rect.width() - size) / 2, rect.y() + (rect.height() - size) / 2, size, size); rect.x() + (rect.width() - size) / 2, rect.y() + (rect.height() - size) / 2, size, size);
} }
virtual void RenderForeground(Renderer &renderer, void RenderForeground(Renderer &renderer,
Rectangle &rect, Rectangle &rect,
IdenticonStyle &style, const ColorTheme &colorTheme,
ColorTheme &colorTheme, const QByteArray &hash)
QString &hash)
{ {
Q_UNUSED(style);
// Ensure rect is quadratic and a multiple of the cell count // Ensure rect is quadratic and a multiple of the cell count
auto normalizedRect = normalizeRectangle(rect); auto normalizedRect = normalizeRectangle(rect);
qDebug() << "cellCount" << cellCount(); qDebug() << "cellCount" << cellCount();
...@@ -99,12 +76,11 @@ protected: ...@@ -99,12 +76,11 @@ protected:
} }
} }
static uint32_t hashQString(const QString &input); static qreal getHue(const QByteArray &input)
static qreal getHue(const QString &input)
{ {
// Create a color for the input // Create a color for the input
auto hash = hashQString(input); uint32_t hash =
(input.at(0) << 24) ^ (input.at(1) << 16) ^ (input.at(2) << 8) ^ input.at(3);
// create a hue value based on the hash of the input. // create a hue value based on the hash of the input.
// Adapted to make Nico blue // Adapted to make Nico blue
auto userHue = static_cast<double>(hash - static_cast<uint32_t>(0x60000000)) / auto userHue = static_cast<double>(hash - static_cast<uint32_t>(0x60000000)) /
...@@ -112,18 +88,20 @@ protected: ...@@ -112,18 +88,20 @@ protected:
return userHue; return userHue;
} }
static char getOctet(QString &arr, const int index) static uint8_t getOctet(const QByteArray &arr, const int index)
{ {
char at = arr.at(index).toLatin1(); uint8_t byte_ = arr.at(index / 2);
char decval = (at >= 'A') ? (at - 'A' + 10) : (at - '0'); if (index % 2 == 0)
return decval; byte_ &= 0x0f;
else
byte_ >>= 4;
return byte_;
} }
public: public:
IconGenerator(); IconGenerator();
virtual int cellCount() { return 4; } int cellCount() const { return 4; }
void generate(Renderer &renderer, Rectangle &rect, IdenticonStyle &style, QString &hash); void generate(Renderer &renderer, Rectangle &rect, QString &hash);
virtual ~IconGenerator() = default;
}; };
} // namespace rendering } // namespace rendering
......
...@@ -3,15 +3,14 @@ ...@@ -3,15 +3,14 @@
namespace shapes { namespace shapes {
Shape::Shape(void (*definition)(rendering::Renderer &, int cell, int index), Shape::Shape(void (*definition)(rendering::Renderer &, int cell, int index),
QColor &color, QColor color,
QList<QPoint> &positions, QList<QPoint> &positions,
int startRotationIndex) int startRotationIndex)
{ : shapeColor_(color)
definition_ = definition; , positions_(positions)
shapeColor_ = color; , startRotationIndex_(startRotationIndex)
positions_ = positions; , definition_(definition)
startRotationIndex_ = startRotationIndex; {}
}
QColor QColor
Shape::getShapeColor() Shape::getShapeColor()
......
...@@ -19,7 +19,7 @@ private: ...@@ -19,7 +19,7 @@ private:
public: public:
Shape(void (*definition)(rendering::Renderer &renderer, int cell, int index), Shape(void (*definition)(rendering::Renderer &renderer, int cell, int index),
QColor &color, QColor color,
QList<QPoint> &positions, QList<QPoint> &positions,
int startRotationIndex); int startRotationIndex);
QColor getShapeColor(); QColor getShapeColor();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment