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 \
src/jdenticonplugin.h \
includes/jdenticoninterface.h
SOURCES += src/identicon.cpp \
src/identiconstyle.cpp \
src/qtidenticon.cpp \
src/rendering/colortheme.cpp \
src/rendering/colorutils.cpp \
......
#include "identicon.h"
IdenticonStyle Identicon::defaultStyle_ = {};
Identicon::Identicon(QString &hash, int size)
{
size_ = size;
hash_ = hash;
}
Identicon
Identicon::fromHash(QByteArray &hash, int size)
{
QString str = QString(hash);
Identicon fromHash(str, size);
return fromHash;
}
Identicon::Identicon(const QString &hash, int size)
: hash_(hash)
, size_(size)
{}
QString
Identicon::generateSvg(Identicon &icon, bool fragment)
......@@ -28,7 +18,7 @@ rendering::Rectangle
Identicon::getIconBounds()
{
// 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);
}
......@@ -36,5 +26,5 @@ Identicon::getIconBounds()
void
Identicon::draw(rendering::Renderer &renderer, rendering::Rectangle &rect)
{
iconGenerator_.generate(renderer, rect, defaultStyle_, hash_);
iconGenerator_.generate(renderer, rect, hash_);
}
......@@ -15,14 +15,11 @@ private:
int size_;
rendering::IconGenerator iconGenerator_;
IdenticonStyle style_;
static IdenticonStyle defaultStyle_;
public:
Identicon(QString &hash, int size);
Identicon(const QString &hash, int size);
int size() { return size_; }
rendering::Rectangle getIconBounds();
static Identicon fromHash(QByteArray &hash, int size);
static Identicon fromHash(QString &hash, int size);
static QString generateSvg(Identicon &identicon, bool fragment);
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 @@
#define IDENTICONSTYLE_H
#include <QColor>
#include <QList>
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:
IdenticonStyle();
IdenticonStyle(IdenticonStyle &other);
QList<qreal> hues() { return hues_; }
QColor backCol() { return backColor_; }
qreal padding() { return padding_; }
qreal colorSaturation() { return colorSaturation_; }
qreal grayscaleSaturation() { return grayscaleSaturation_; }
qreal minColorLightness() { return minColorLightness_; }
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);
static QColor backCol() { return QColor(0, 0, 0, 0); }
static qreal padding() { return 0.08; }
static qreal colorSaturation() { return 0.5; }
static qreal grayscaleSaturation() { return 0.0; }
static qreal minColorLightness() { return 0.4; }
static qreal maxColorLightness() { return 0.8; }
static qreal minGrayLightness() { return 0.3; }
static qreal maxGrayLightness() { return 0.9; }
};
#endif // IDENTICONSTYLE_H
......@@ -5,8 +5,6 @@
QString
JdenticonPlugin::generate(const QString &message, uint16_t size)
{
//QByteArray byteArr = QByteArray::fromStdString(message.toStdString());
QString copy = message;
Identicon identicon(copy, size);
Identicon identicon(message, size);
return identicon.generateSvg(identicon, false);
}
#include <stdexcept>
#include "colortheme.h"
#include <stdexcept>
#include "../identiconstyle.h"
namespace rendering {
ColorTheme::ColorTheme(qreal hue, IdenticonStyle &style)
ColorTheme::ColorTheme(qreal hue)
{
if (style.hues().size() > 0) {
// Multiply with 0.999 to change the range to [0, 1)
int hueIndex = qFloor(0.999 * hue * style.hues().size());
hue = style.hues().at(hueIndex);
}
darkGray_ =
ColorUtils::fromHslCompensated(hue, style.grayscaleSaturation(), style.minGrayLightness());
darkGray_ = ColorUtils::fromHslCompensated(
hue, IdenticonStyle::grayscaleSaturation(), IdenticonStyle::minGrayLightness());
midColor_ = ColorUtils::fromHslCompensated(
hue, style.colorSaturation(), (style.minColorLightness() + style.maxColorLightness()) / 2);
lightGray_ =
ColorUtils::fromHslCompensated(hue, style.grayscaleSaturation(), style.maxGrayLightness());
lightColor_ =
ColorUtils::fromHslCompensated(hue, style.colorSaturation(), style.maxColorLightness());
darkColor_ =
ColorUtils::fromHslCompensated(hue, style.colorSaturation(), style.minColorLightness());
hue,
IdenticonStyle::colorSaturation(),
(IdenticonStyle::minColorLightness() + IdenticonStyle::maxColorLightness()) / 2);
lightGray_ = ColorUtils::fromHslCompensated(
hue, IdenticonStyle::grayscaleSaturation(), IdenticonStyle::maxGrayLightness());
lightColor_ = ColorUtils::fromHslCompensated(
hue, IdenticonStyle::colorSaturation(), IdenticonStyle::maxColorLightness());
darkColor_ = ColorUtils::fromHslCompensated(
hue, IdenticonStyle::colorSaturation(), IdenticonStyle::minColorLightness());
}
QColor &
ColorTheme::operator[](int index)
QColor
ColorTheme::operator[](int index) const
{
if (index == 0)
return darkGray_;
......
......@@ -3,7 +3,6 @@
#include <QColor>
#include "../identiconstyle.h"
#include "colorutils.h"
namespace rendering {
......@@ -19,9 +18,9 @@ private:
QColor lightColor_;
public:
ColorTheme(qreal hue, IdenticonStyle &style);
int count() { return 5; }
QColor &operator[](int index);
ColorTheme(qreal hue);
int count() const { return 5; }
QColor operator[](int index) const;
};
} // namespace rendering
......
#include <stdexcept>
#include "colorutils.h"
#include <stdexcept>
......
......@@ -3,22 +3,22 @@
#include <QCryptographicHash>
namespace rendering {
QList<QPoint> IconGenerator::shapeOne_({QPoint(1, 0),
QPoint(2, 0),
QPoint(2, 3),
QPoint(1, 3),
QPoint(0, 1),
QPoint(3, 1),
QPoint(3, 2),
QPoint(0, 2)});
QList<QPoint> IconGenerator::shapeTwo_({{0, 0}, {3, 0}, {3, 3}, {0, 3}});
QList<QPoint> IconGenerator::shapeThree_({{1, 1}, {2, 1}, {2, 2}, {1, 2}});
QList<void (*)(rendering::Renderer &, int, int)> IconGenerator::centerShapes_ =
static QList<QPoint> shapeOne_({QPoint(1, 0),
QPoint(2, 0),
QPoint(2, 3),
QPoint(1, 3),
QPoint(0, 1),
QPoint(3, 1),
QPoint(3, 2),
QPoint(0, 2)});
static QList<QPoint> shapeTwo_({{0, 0}, {3, 0}, {3, 3}, {0, 3}});
static QList<QPoint> shapeThree_({{1, 1}, {2, 1}, {2, 2}, {1, 2}});
static QList<void (*)(rendering::Renderer &, int, int)> centerShapes_ =
shapes::ShapeDefinitions::CenterShapes();
QList<void (*)(rendering::Renderer &, int, int)> IconGenerator::outerShapes_ =
static QList<void (*)(rendering::Renderer &, int, int)> outerShapes_ =
shapes::ShapeDefinitions::OuterShapes();
QList<shapes::ShapeCategory> IconGenerator::defaultCategories_ = {
static QList<shapes::ShapeCategory> defaultCategories_ = {
// Sides
shapes::ShapeCategory(8, 2, 3, shapeOne_, outerShapes_),
// Corner
......@@ -35,7 +35,7 @@ IconGenerator::getCategories()
}
QList<shapes::Shape>
IconGenerator::getShapes(ColorTheme &theme, QString &hash)
IconGenerator::getShapes(const ColorTheme &theme, const QByteArray &hash)
{
QList<shapes::Shape> shapes;
QList<int> usedColorThemeIndexes;
......@@ -73,24 +73,16 @@ IconGenerator::getShapes(ColorTheme &theme, QString &hash)
}
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;
auto colorTheme = ColorTheme(hue, style);
QString hash =
QString(QCryptographicHash::hash(input.toUtf8(), QCryptographicHash::Sha1).toHex());
auto colorTheme = ColorTheme(hue);
RenderBackground(renderer, rect, style, colorTheme, hash);
RenderForeground(renderer, rect, style, colorTheme, hash);
RenderBackground(renderer);
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
......@@ -13,18 +13,9 @@
namespace rendering {
class IconGenerator
class IconGenerator final
{
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)) {
......@@ -39,21 +30,9 @@ private:
protected:
QList<shapes::ShapeCategory> getCategories();
virtual QList<shapes::Shape> getShapes(ColorTheme &theme, QString &hash);
virtual void RenderBackground(Renderer &renderer,
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)
QList<shapes::Shape> getShapes(const ColorTheme &theme, const QByteArray &hash);
void RenderBackground(Renderer &renderer) { renderer.setBackgroundColor(QColor(0, 0, 0, 0)); }
Rectangle normalizeRectangle(Rectangle &rect)
{
auto size = qMin(rect.width(), rect.height());
......@@ -63,13 +42,11 @@ protected:
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,
QString &hash)
void RenderForeground(Renderer &renderer,
Rectangle &rect,
const ColorTheme &colorTheme,
const QByteArray &hash)
{
Q_UNUSED(style);
// Ensure rect is quadratic and a multiple of the cell count
auto normalizedRect = normalizeRectangle(rect);
qDebug() << "cellCount" << cellCount();
......@@ -99,12 +76,11 @@ protected:
}
}
static uint32_t hashQString(const QString &input);
static qreal getHue(const QString &input)
static qreal getHue(const QByteArray &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.
// Adapted to make Nico blue
auto userHue = static_cast<double>(hash - static_cast<uint32_t>(0x60000000)) /
......@@ -112,18 +88,20 @@ protected:
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();
char decval = (at >= 'A') ? (at - 'A' + 10) : (at - '0');
return decval;
uint8_t byte_ = arr.at(index / 2);
if (index % 2 == 0)
byte_ &= 0x0f;
else
byte_ >>= 4;
return byte_;
}
public:
IconGenerator();
virtual int cellCount() { return 4; }
void generate(Renderer &renderer, Rectangle &rect, IdenticonStyle &style, QString &hash);
virtual ~IconGenerator() = default;
int cellCount() const { return 4; }
void generate(Renderer &renderer, Rectangle &rect, QString &hash);
};
} // namespace rendering
......
......@@ -3,15 +3,14 @@
namespace shapes {
Shape::Shape(void (*definition)(rendering::Renderer &, int cell, int index),
QColor &color,
QColor color,
QList<QPoint> &positions,
int startRotationIndex)
{
definition_ = definition;
shapeColor_ = color;
positions_ = positions;
startRotationIndex_ = startRotationIndex;
}
: shapeColor_(color)
, positions_(positions)
, startRotationIndex_(startRotationIndex)
, definition_(definition)
{}
QColor
Shape::getShapeColor()
......
......@@ -19,7 +19,7 @@ private:
public:
Shape(void (*definition)(rendering::Renderer &renderer, int cell, int index),
QColor &color,
QColor color,
QList<QPoint> &positions,
int startRotationIndex);
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