Skip to content

Commit 6aa1cb2

Browse files
committed
Use Signal-Android's method of determining user color
1 parent 0dfe227 commit 6aa1cb2

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

Signal-Windows/Storage/DB.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ public static SignalContact GetOrCreateContactLocked(string username, long times
11301130
ThreadDisplayName = username,
11311131
CanReceive = true,
11321132
LastActiveTimestamp = timestamp,
1133-
Color = Utils.Colors[Utils.CalculateDefaultColorIndex(username)]
1133+
Color = Utils.CalculateDefaultColor(username)
11341134
};
11351135
ctx.Contacts.Add(contact);
11361136
ctx.SaveChanges();

Signal-Windows/Utils.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,27 @@ public static void AddRange<T>(this ObservableCollection<T> observableCollection
9494
}
9595
}
9696

97-
public static int CalculateDefaultColorIndex(string title)
97+
public static string CalculateDefaultColor(string title)
9898
{
99-
if (title.Length == 0)
100-
{
101-
return 0;
102-
}
103-
var hash = 0;
104-
for (int i = 0; i < title.Length; i++)
99+
return Colors[Math.Abs(JavaStringHashCode(title)) % Colors.Length];
100+
}
101+
102+
public static SolidColorBrush GetDefaultColor(string title)
103+
{
104+
return GetBrushFromColor(CalculateDefaultColor(title));
105+
}
106+
107+
public static int JavaStringHashCode(string str)
108+
{
109+
int h = 0;
110+
if (str.Length > 0)
105111
{
106-
hash = ((hash << 5) - hash) + title[i];
107-
hash = hash & hash;
112+
for (int i = 0; i < str.Length; i++)
113+
{
114+
h = 31 * h + str[i];
115+
}
108116
}
109-
return Math.Abs(hash) % 15;
117+
return h;
110118
}
111119

112120
public static void EnableBackButton()
@@ -949,4 +957,4 @@ public static class CountryArrays
949957
"ZW"
950958
};
951959
};
952-
}
960+
}

Signal-Windows/ViewModels/AddContactPageViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private async Task AddContact(string name, string number)
316316
AvatarFile = null,
317317
LastActiveTimestamp = 0,
318318
Draft = null,
319-
Color = "red",
319+
Color = Utils.CalculateDefaultColor(name),
320320
UnreadCount = 0
321321
};
322322
await Task.Run(() =>
@@ -337,4 +337,4 @@ private string ParsePhoneNumber(string number)
337337
return phoneNumberUtil.Format(phoneNumber, PhoneNumberFormat.E164);
338338
}
339339
}
340-
}
340+
}

0 commit comments

Comments
 (0)