Port stringToColor to C++.

Fixes #50.
square-messages
Black Hat 2018-09-16 16:09:36 +08:00
parent 03a8eae491
commit 1e04013e3d
11 changed files with 20 additions and 27 deletions

View File

@ -1,18 +1,5 @@
.pragma library
function stringToColor(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
var colour = '#';
for (var j = 0; j < 3; j++) {
var value = (hash >> (j * 8)) & 0xFF;
colour += ('00' + value.toString(16)).substr(-2);
}
return colour;
}
function pushToStack(stack, page) {
if(page && stack.currentItem !== page) {
if(stack.depth === 1) {

View File

@ -54,7 +54,6 @@ Page {
height: parent.height
hint: user.displayName
defaultColor: Util.stringToColor(user.displayName)
image: user.avatar
}

View File

@ -11,7 +11,7 @@ Text {
verticalAlignment: Text.AlignVCenter
font.pointSize: 20
font.family: "Noto Color Emoji"
font.family: "Emoji"
MouseArea {
anchors.fill: parent

View File

@ -37,7 +37,7 @@ Popup {
verticalAlignment: Text.AlignVCenter
font.pointSize: 20
font.family: "Noto Color Emoji"
font.family: "Emoji"
text: modelData
MouseArea {

View File

@ -31,7 +31,6 @@ RowLayout {
round: false
visible: avatarVisible
hint: author.displayName
defaultColor: Util.stringToColor(author.displayName)
image: author.avatar
}
@ -106,7 +105,6 @@ RowLayout {
height: parent.height
hint: modelData.displayName
defaultColor: Util.stringToColor(modelData.displayName)
image: modelData.avatar
}
}

View File

@ -30,7 +30,6 @@ Drawer {
Layout.alignment: Qt.AlignHCenter
hint: room ? room.displayName : "No name"
defaultColor: Util.stringToColor(room ? room.displayName : "No name")
image: matriqueController.safeImage(room ? room.avatar : null)
}
@ -123,7 +122,6 @@ Drawer {
Layout.preferredWidth: height
Layout.fillHeight: true
defaultColor: Util.stringToColor(name)
image: avatar
hint: name
}

View File

@ -63,7 +63,6 @@ Item {
Layout.fillHeight: true
hint: currentRoom ? currentRoom.displayName : "No name"
defaultColor: Util.stringToColor(currentRoom ? currentRoom.displayName : "No name")
image: matriqueController.safeImage(currentRoom ? currentRoom.avatar : null)
}
@ -292,7 +291,6 @@ Item {
Layout.preferredWidth: height
Layout.fillHeight: true
defaultColor: Util.stringToColor(modelData.displayName)
image: modelData.avatar
hint: modelData.displayName
}

View File

@ -164,7 +164,6 @@ Item {
Layout.fillHeight: true
hint: name || "No Name"
defaultColor: Util.stringToColor(name || "No Name")
image: avatar
}

View File

@ -128,7 +128,6 @@ ApplicationWindow {
hint: user.displayName
image: user.avatar
defaultColor: Util.stringToColor(user.displayName)
}
highlightColor: matriqueController.color(user.id)

View File

@ -13,7 +13,10 @@ void ImageItem::paint(QPainter *painter) {
if (m_image.isNull()) {
painter->setPen(Qt::NoPen);
painter->setBrush(QColor(m_color));
if (m_color.isEmpty())
painter->setBrush(QColor(stringtoColor(m_hint)));
else
painter->setBrush(QColor(m_color));
if (m_round)
painter->drawEllipse(0, 0, int(bounding_rect.width()),
int(bounding_rect.height()));
@ -22,7 +25,7 @@ void ImageItem::paint(QPainter *painter) {
int(bounding_rect.height()));
painter->setPen(QPen(Qt::white, 2));
QFont font;
font.setPixelSize(bounding_rect.width() / 2);
font.setPixelSize(int(bounding_rect.width() / 2));
font.setBold(true);
painter->setFont(font);
painter->drawText(
@ -80,3 +83,13 @@ void ImageItem::setRound(bool value) {
update();
}
}
QString ImageItem::stringtoColor(QString string) {
int hash = 0;
for (int i = 0; i < string.length(); i++)
hash = string.at(i).unicode() + ((hash << 5) - hash);
QString colour = "#";
for (int j = 0; j < 3; j++)
colour += ("00" + QString::number((hash >> (j * 8)) & 0xFF, 16)).right(2);
return colour;
}

View File

@ -41,8 +41,10 @@ class ImageItem : public QQuickPaintedItem {
private:
QImage m_image;
QString m_hint = "H";
QString m_color = "#000000";
QString m_color;
bool m_round = true;
QString stringtoColor(QString string);
};
#endif // IMAGEITEM_H