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

Update jdenticon to use nheko color logic and fix octet functionality

parent 09cee7ef
No related branches found
No related tags found
1 merge request!1Fix randomization issues
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
module im.nheko.qtjdenticon
plugin qt-jdenticon
#include "identicon.h"
IdenticonStyle Identicon::defaultStyle_ = {};
Identicon::Identicon(QByteArray &hash, int size)
Identicon::Identicon(QString &hash, int size)
{
size_ = size;
hash_ = hash;
......@@ -10,18 +10,11 @@ Identicon::Identicon(QByteArray &hash, int size)
Identicon
Identicon::fromHash(QByteArray &hash, int size)
{
Identicon fromHash(hash, size);
QString str = QString(hash);
Identicon fromHash(str, size);
return fromHash;
}
Identicon
Identicon::fromHash(QString &hash, int size)
{
QByteArray hashArr = QByteArray::fromStdString(hash.toStdString());
Identicon identicon(hashArr, size);
return identicon;
}
QString
Identicon::generateSvg(Identicon &icon, bool fragment)
{
......
......@@ -11,14 +11,14 @@
class Identicon
{
private:
QByteArray hash_;
QString hash_;
int size_;
rendering::IconGenerator iconGenerator_;
IdenticonStyle style_;
static IdenticonStyle defaultStyle_;
public:
Identicon(QByteArray &hash, int size);
Identicon(QString &hash, int size);
int size() { return size_; }
rendering::Rectangle getIconBounds();
static Identicon fromHash(QByteArray &hash, int size);
......
......@@ -5,7 +5,8 @@
QString
JdenticonPlugin::generate(const QString &message, uint16_t size)
{
QByteArray byteArr = QByteArray::fromStdString(message.toStdString());
Identicon identicon(byteArr, size);
//QByteArray byteArr = QByteArray::fromStdString(message.toStdString());
QString copy = message;
Identicon identicon(copy, size);
return identicon.generateSvg(identicon, false);
}
......@@ -13,7 +13,7 @@ main(int argc, char *argv[])
};
for (QString hash : hashes) {
QByteArray hashArr = QByteArray::fromStdString(hash.toStdString());
Identicon test(hashArr, 256);
Identicon test(hash, 256);
qInfo() << hash;
qInfo() << test.generateSvg(test, false);
}
......
#include "icongenerator.h"
#include <QCryptographicHash>
namespace rendering {
QList<QPoint> IconGenerator::shapeOne_({QPoint(1, 0),
......@@ -34,7 +35,7 @@ IconGenerator::getCategories()
}
QList<shapes::Shape>
IconGenerator::getShapes(ColorTheme &theme, QByteArray &hash)
IconGenerator::getShapes(ColorTheme &theme, QString &hash)
{
QList<shapes::Shape> shapes;
QList<int> usedColorThemeIndexes;
......@@ -63,6 +64,7 @@ IconGenerator::getShapes(ColorTheme &theme, QByteArray &hash)
auto definitionSize = category.getDefinitions().size();
qDebug() << "definitionSize " << definitionSize;
auto shape = category.getDefinitions()[octet % definitionSize];
qDebug() << "Shape # " << (octet % definitionSize) ;
auto positions = category.getPositions();
shapes::Shape newShape = {shape, theme[colorThemeIndex], positions, startRotationIndex};
shapes.append(newShape);
......@@ -74,12 +76,13 @@ void
IconGenerator::generate(Renderer &renderer,
Rectangle &rect,
IdenticonStyle &style,
QByteArray &hash)
QString &input)
{
auto hue = getHue(hash);
auto hue = getHue(input);
qDebug() << "hue" << hue;
auto colorTheme = ColorTheme(hue, style);
QString hash = QString(QCryptographicHash::hash(input.toUtf8(),QCryptographicHash::Sha1).toHex());
RenderBackground(renderer, rect, style, colorTheme, hash);
RenderForeground(renderer, rect, style, colorTheme, hash);
......
#ifndef ICONGENERATOR_H
#define ICONGENERATOR_H
#include <QString>
#include <QList>
#include <QtDebug>
......@@ -38,12 +39,12 @@ private:
protected:
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,
Rectangle rect,
IdenticonStyle &style,
ColorTheme &colorTheme,
QByteArray &hash)
QString &hash)
{
Q_UNUSED(rect);
Q_UNUSED(style);
......@@ -66,7 +67,7 @@ protected:
Rectangle &rect,
IdenticonStyle &style,
ColorTheme &colorTheme,
QByteArray &hash)
QString &hash)
{
Q_UNUSED(style);
// Ensure rect is quadratic and a multiple of the cell count
......@@ -97,9 +98,10 @@ protected:
// 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++) {
hash = input.at(i).digitValue() + ((hash << 5) - hash);
......@@ -107,34 +109,27 @@ protected:
return hash;
}
static qreal getHue(QByteArray &hash)
static qreal getHue(const QString &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.
auto userHue = qAbs(hashInt % 360);
auto userHue = hash % 360;
return userHue / 360.0;
}
static char getOctet(QByteArray &arr, const int index)
static char getOctet(QString &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;
char at = arr.at(index).toLatin1();
char decval = (at >= 'A') ? (at - 'A' + 10) : (at - '0');
return decval;
}
public:
IconGenerator();
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;
};
......
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