Port stringToColor to C++.

Fixes #50.
This commit is contained in:
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 .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) { function pushToStack(stack, page) {
if(page && stack.currentItem !== page) { if(page && stack.currentItem !== page) {
if(stack.depth === 1) { if(stack.depth === 1) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,6 +13,9 @@ void ImageItem::paint(QPainter *painter) {
if (m_image.isNull()) { if (m_image.isNull()) {
painter->setPen(Qt::NoPen); painter->setPen(Qt::NoPen);
if (m_color.isEmpty())
painter->setBrush(QColor(stringtoColor(m_hint)));
else
painter->setBrush(QColor(m_color)); painter->setBrush(QColor(m_color));
if (m_round) if (m_round)
painter->drawEllipse(0, 0, int(bounding_rect.width()), painter->drawEllipse(0, 0, int(bounding_rect.width()),
@ -22,7 +25,7 @@ void ImageItem::paint(QPainter *painter) {
int(bounding_rect.height())); int(bounding_rect.height()));
painter->setPen(QPen(Qt::white, 2)); painter->setPen(QPen(Qt::white, 2));
QFont font; QFont font;
font.setPixelSize(bounding_rect.width() / 2); font.setPixelSize(int(bounding_rect.width() / 2));
font.setBold(true); font.setBold(true);
painter->setFont(font); painter->setFont(font);
painter->drawText( painter->drawText(
@ -80,3 +83,13 @@ void ImageItem::setRound(bool value) {
update(); 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: private:
QImage m_image; QImage m_image;
QString m_hint = "H"; QString m_hint = "H";
QString m_color = "#000000"; QString m_color;
bool m_round = true; bool m_round = true;
QString stringtoColor(QString string);
}; };
#endif // IMAGEITEM_H #endif // IMAGEITEM_H