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

Fix randomization issues

parent e58c1436
No related branches found
No related tags found
1 merge request!1Fix randomization issues
...@@ -3,8 +3,11 @@ ...@@ -3,8 +3,11 @@
###################################################################### ######################################################################
TEMPLATE = lib TEMPLATE = lib
CONFIG += plugin CONFIG += plugin c++11
TARGET = qtjdenticon TARGET = qtjdenticon
TARGET = $$qtLibraryTarget($$TARGET)
uri = im.nheko.qtjdenticon
INCLUDEPATH = includes INCLUDEPATH = includes
INCLUDEPATH += . INCLUDEPATH += .
VERSION = 0.1.0 VERSION = 0.1.0
......
#include "identicon.h" #include "identicon.h"
IdenticonStyle Identicon::defaultStyle_ = {}; IdenticonStyle Identicon::defaultStyle_ = {};
Identicon::Identicon(QByteArray &hash, int size) Identicon::Identicon(QString &hash, int size)
{ {
size_ = size; size_ = size;
hash_ = hash; hash_ = hash;
...@@ -10,18 +10,11 @@ Identicon::Identicon(QByteArray &hash, int size) ...@@ -10,18 +10,11 @@ Identicon::Identicon(QByteArray &hash, int size)
Identicon Identicon
Identicon::fromHash(QByteArray &hash, int size) Identicon::fromHash(QByteArray &hash, int size)
{ {
Identicon fromHash(hash, size); QString str = QString(hash);
Identicon fromHash(str, size);
return fromHash; return fromHash;
} }
Identicon
Identicon::fromHash(QString &hash, int size)
{
QByteArray hashArr = QByteArray::fromStdString(hash.toStdString());
Identicon identicon(hashArr, size);
return identicon;
}
QString QString
Identicon::generateSvg(Identicon &icon, bool fragment) Identicon::generateSvg(Identicon &icon, bool fragment)
{ {
......
...@@ -11,14 +11,14 @@ ...@@ -11,14 +11,14 @@
class Identicon class Identicon
{ {
private: private:
QByteArray hash_; QString hash_;
int size_; int size_;
rendering::IconGenerator iconGenerator_; rendering::IconGenerator iconGenerator_;
IdenticonStyle style_; IdenticonStyle style_;
static IdenticonStyle defaultStyle_; static IdenticonStyle defaultStyle_;
public: public:
Identicon(QByteArray &hash, int size); Identicon(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(QByteArray &hash, int size);
......
#include <stdexcept>
#include "identiconstyle.h" #include "identiconstyle.h"
#include <stdexcept> #include <stdexcept>
......
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
QString QString
JdenticonPlugin::generate(const QString &message, uint16_t size) JdenticonPlugin::generate(const QString &message, uint16_t size)
{ {
QByteArray byteArr = QByteArray::fromStdString(message.toStdString()); //QByteArray byteArr = QByteArray::fromStdString(message.toStdString());
Identicon identicon(byteArr, size); QString copy = message;
Identicon identicon(copy, size);
return identicon.generateSvg(identicon, false); return identicon.generateSvg(identicon, false);
} }
...@@ -11,7 +11,7 @@ class JdenticonPlugin ...@@ -11,7 +11,7 @@ class JdenticonPlugin
, JdenticonInterface , JdenticonInterface
{ {
Q_OBJECT Q_OBJECT
Q_PLUGIN_METADATA(IID "redsky17.Qt.JdenticonInterface") Q_PLUGIN_METADATA(IID "im.nheko.JdenticonInterface")
Q_INTERFACES(JdenticonInterface) Q_INTERFACES(JdenticonInterface)
public: public:
......
...@@ -13,7 +13,7 @@ main(int argc, char *argv[]) ...@@ -13,7 +13,7 @@ main(int argc, char *argv[])
}; };
for (QString hash : hashes) { for (QString hash : hashes) {
QByteArray hashArr = QByteArray::fromStdString(hash.toStdString()); QByteArray hashArr = QByteArray::fromStdString(hash.toStdString());
Identicon test(hashArr, 256); Identicon test(hash, 256);
qInfo() << hash; qInfo() << hash;
qInfo() << test.generateSvg(test, false); qInfo() << test.generateSvg(test, false);
} }
......
#include <stdexcept>
#include "colortheme.h" #include "colortheme.h"
#include <stdexcept> #include <stdexcept>
......
#include <stdexcept>
#include "colorutils.h" #include "colorutils.h"
#include <stdexcept> #include <stdexcept>
......
#include "icongenerator.h" #include "icongenerator.h"
#include <QCryptographicHash>
namespace rendering { namespace rendering {
QList<QPoint> IconGenerator::shapeOne_({QPoint(1, 0), QList<QPoint> IconGenerator::shapeOne_({QPoint(1, 0),
...@@ -34,7 +35,7 @@ IconGenerator::getCategories() ...@@ -34,7 +35,7 @@ IconGenerator::getCategories()
} }
QList<shapes::Shape> QList<shapes::Shape>
IconGenerator::getShapes(ColorTheme &theme, QByteArray &hash) IconGenerator::getShapes(ColorTheme &theme, QString &hash)
{ {
QList<shapes::Shape> shapes; QList<shapes::Shape> shapes;
QList<int> usedColorThemeIndexes; QList<int> usedColorThemeIndexes;
...@@ -63,6 +64,7 @@ IconGenerator::getShapes(ColorTheme &theme, QByteArray &hash) ...@@ -63,6 +64,7 @@ IconGenerator::getShapes(ColorTheme &theme, QByteArray &hash)
auto definitionSize = category.getDefinitions().size(); auto definitionSize = category.getDefinitions().size();
qDebug() << "definitionSize " << definitionSize; qDebug() << "definitionSize " << definitionSize;
auto shape = category.getDefinitions()[octet % definitionSize]; auto shape = category.getDefinitions()[octet % definitionSize];
qDebug() << "Shape # " << (octet % definitionSize) ;
auto positions = category.getPositions(); auto positions = category.getPositions();
shapes::Shape newShape = {shape, theme[colorThemeIndex], positions, startRotationIndex}; shapes::Shape newShape = {shape, theme[colorThemeIndex], positions, startRotationIndex};
shapes.append(newShape); shapes.append(newShape);
...@@ -74,12 +76,13 @@ void ...@@ -74,12 +76,13 @@ void
IconGenerator::generate(Renderer &renderer, IconGenerator::generate(Renderer &renderer,
Rectangle &rect, Rectangle &rect,
IdenticonStyle &style, IdenticonStyle &style,
QByteArray &hash) QString &input)
{ {
auto hue = getHue(hash); auto hue = getHue(input);
qDebug() << "hue" << hue; qDebug() << "hue" << hue;
auto colorTheme = ColorTheme(hue, style); auto colorTheme = ColorTheme(hue, style);
QString hash = QString(QCryptographicHash::hash(input.toUtf8(),QCryptographicHash::Sha1).toHex());
RenderBackground(renderer, rect, style, colorTheme, hash); RenderBackground(renderer, rect, style, colorTheme, hash);
RenderForeground(renderer, rect, style, colorTheme, hash); RenderForeground(renderer, rect, style, colorTheme, hash);
......
#ifndef ICONGENERATOR_H #ifndef ICONGENERATOR_H
#define ICONGENERATOR_H #define ICONGENERATOR_H
#include <QString>
#include <QList> #include <QList>
#include <QtDebug> #include <QtDebug>
...@@ -38,12 +39,12 @@ private: ...@@ -38,12 +39,12 @@ private:
protected: protected:
QList<shapes::ShapeCategory> getCategories(); QList<shapes::ShapeCategory> getCategories();
virtual QList<shapes::Shape> getShapes(ColorTheme &theme, QByteArray &hash); virtual QList<shapes::Shape> getShapes(ColorTheme &theme, QString &hash);
virtual void RenderBackground(Renderer &renderer, virtual void RenderBackground(Renderer &renderer,
Rectangle rect, Rectangle rect,
IdenticonStyle &style, IdenticonStyle &style,
ColorTheme &colorTheme, ColorTheme &colorTheme,
QByteArray &hash) QString &hash)
{ {
Q_UNUSED(rect); Q_UNUSED(rect);
Q_UNUSED(style); Q_UNUSED(style);
...@@ -66,7 +67,7 @@ protected: ...@@ -66,7 +67,7 @@ protected:
Rectangle &rect, Rectangle &rect,
IdenticonStyle &style, IdenticonStyle &style,
ColorTheme &colorTheme, ColorTheme &colorTheme,
QByteArray &hash) QString &hash)
{ {
Q_UNUSED(style); 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
...@@ -97,9 +98,10 @@ protected: ...@@ -97,9 +98,10 @@ protected:
// renderer.endShape(); // renderer.endShape();
} }
} }
static int hashQString(const QString &input)
static uint32_t hashQString(const QString &input)
{ {
auto hash = 0; uint32_t hash = 0;
for (int i = 0; i < input.length(); i++) { for (int i = 0; i < input.length(); i++) {
hash = input.at(i).digitValue() + ((hash << 5) - hash); hash = input.at(i).digitValue() + ((hash << 5) - hash);
...@@ -107,34 +109,27 @@ protected: ...@@ -107,34 +109,27 @@ protected:
return hash; return hash;
} }
static qreal getHue(QByteArray &hash)
static qreal getHue(const QString &input)
{ {
// Create a color for the input // Create a color for the input
auto hashInt = hashQString(hash); auto hash = hashQString(input);
// create a hue value based on the hash of the input. // create a hue value based on the hash of the input.
auto userHue = qAbs(hashInt % 360); auto userHue = hash % 360;
return userHue / 360.0; return userHue / 360.0;
} }
static char getOctet(QByteArray &arr, const int index) static char getOctet(QString &arr, const int index)
{ {
auto byteIndex = index / 2; char at = arr.at(index).toLatin1();
auto byteValue = arr.at(byteIndex); char decval = (at >= 'A') ? (at - 'A' + 10) : (at - '0');
return decval;
if (byteIndex * 2 == index) {
byteValue = byteValue >> 4;
} else {
byteValue = byteValue & 0xf;
}
return byteValue;
} }
public: public:
IconGenerator(); IconGenerator();
virtual int cellCount() { return 4; } virtual int cellCount() { return 4; }
void generate(Renderer &renderer, Rectangle &rect, IdenticonStyle &style, QByteArray &hash); void generate(Renderer &renderer, Rectangle &rect, IdenticonStyle &style, QString &hash);
virtual ~IconGenerator() = default; virtual ~IconGenerator() = default;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment