Changed rainbow algorithm

This commit is contained in:
Constantin Wartenburger 2020-10-05 17:28:05 +02:00
parent 5fa281dd3a
commit 26b8ef8af9
No known key found for this signature in database
GPG key ID: 7439D96D8E1DB894
3 changed files with 83 additions and 79 deletions

View file

@ -4,6 +4,7 @@ Changes in Element 1.0.9 (2020-XX-XX)
Features ✨: Features ✨:
- Search messages in a room - phase 1 (#2110) - Search messages in a room - phase 1 (#2110)
- Hide encrypted history (before user is invited). Can be shown if wanted in developer settings - Hide encrypted history (before user is invited). Can be shown if wanted in developer settings
- Changed rainbow algorithm
Improvements 🙌: Improvements 🙌:
- Wording differentiation for direct rooms (#2176) - Wording differentiation for direct rooms (#2176)

View file

@ -18,8 +18,10 @@ package im.vector.app.features.home.room.detail.composer.rainbow
import im.vector.app.core.utils.splitEmoji import im.vector.app.core.utils.splitEmoji
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.abs import kotlin.math.cos
import kotlin.math.pow
import kotlin.math.roundToInt import kotlin.math.roundToInt
import kotlin.math.sin
/** /**
* Inspired from React-Sdk * Inspired from React-Sdk
@ -29,7 +31,7 @@ class RainbowGenerator @Inject constructor() {
fun generate(text: String): String { fun generate(text: String): String {
val split = text.splitEmoji() val split = text.splitEmoji()
val frequency = 360f / split.size val frequency = 360.0 / split.size
return split return split
.mapIndexed { idx, letter -> .mapIndexed { idx, letter ->
@ -37,53 +39,54 @@ class RainbowGenerator @Inject constructor() {
if (letter == " ") { if (letter == " ") {
"$letter" "$letter"
} else { } else {
val dashColor = hueToRGB(idx * frequency, 1.0f, 0.5f).toDashColor() val (a, b) = generateAB(idx * frequency, 1f)
val dashColor = labToRGB(75, a, b).toDashColor()
"<font color=\"$dashColor\">$letter</font>" "<font color=\"$dashColor\">$letter</font>"
} }
} }
.joinToString(separator = "") .joinToString(separator = "")
} }
private fun hueToRGB(h: Float, s: Float, l: Float): RgbColor { private fun generateAB(hue: Double, chroma: Float): Pair<Double, Double> {
val c = s * (1 - abs(2 * l - 1)) val radians = Math.toRadians(hue)
val x = c * (1 - abs((h / 60) % 2 - 1))
val m = l - c / 2
var r = 0f val a = chroma * 127 * cos(radians)
var g = 0f val b = chroma * 127 * sin(radians)
var b = 0f
when { return Pair(a, b)
h < 60f -> { }
r = c
g = x private fun labToRGB(l: Int, a: Double, b: Double): RgbColor {
} var y = (l + 16) / 116.0
h < 120f -> { val x = adjustXYZ(y + a / 500) * 0.9505
r = x val z = adjustXYZ(y - b / 200) * 1.0890
g = c
} y = adjustXYZ(y)
h < 180f -> {
g = c val red = 3.24096994 * x - 1.53738318 * y - 0.49861076 * z
b = x val green = -0.96924364 * x + 1.8759675 * y + 0.04155506 * z
} val blue = 0.05563008 * x - 0.20397696 * y + 1.05697151 * z
h < 240f -> {
g = x return RgbColor(adjustRGB(red), adjustRGB(green), adjustRGB(blue))
b = c }
}
h < 300f -> { private fun adjustXYZ(value: Double): Double {
r = x if (value > 0.2069) {
b = c return value.pow(3)
}
else -> {
r = c
b = x
}
} }
return 0.1284 * value - 0.01771
}
return RgbColor( private fun gammaCorrection(value: Double): Double {
((r + m) * 255).roundToInt(), if (value <= 0.0031308) {
((g + m) * 255).roundToInt(), return 12.92 * value
((b + m) * 255).roundToInt() }
) return 1.055 * value.pow(1 / 2.4) - 0.055
}
private fun adjustRGB(value: Double): Int {
return (gammaCorrection(value)
.coerceIn(0.0, 1.0) * 255)
.roundToInt()
} }
} }

View file

@ -32,14 +32,14 @@ class RainbowGeneratorTest {
@Test @Test
fun testAscii1() { fun testAscii1() {
assertEquals("""<font color="#ff0000">a</font>""", rainbowGenerator.generate("a")) assertEquals("""<font color="#ff00be">a</font>""", rainbowGenerator.generate("a"))
} }
@Test @Test
fun testAscii2() { fun testAscii2() {
val expected = """ val expected = """
<font color="#ff0000">a</font> <font color="#ff00be">a</font>
<font color="#00ffff">b</font> <font color="#00e6b6">b</font>
""".trimIndentOneLine() """.trimIndentOneLine()
assertEquals(expected, rainbowGenerator.generate("ab")) assertEquals(expected, rainbowGenerator.generate("ab"))
@ -48,24 +48,24 @@ class RainbowGeneratorTest {
@Test @Test
fun testAscii3() { fun testAscii3() {
val expected = """ val expected = """
<font color="#ff0000">T</font> <font color="#ff00be">T</font>
<font color="#ff5500">h</font> <font color="#ff0072">h</font>
<font color="#ffaa00">i</font> <font color="#ff3b1d">i</font>
<font color="#ffff00">s</font> <font color="#ff7e00">s</font>
<font color="#55ff00">i</font> <font color="#bdc100">i</font>
<font color="#00ff00">s</font> <font color="#64d200">s</font>
<font color="#00ffaa">a</font> <font color="#00e261">a</font>
<font color="#00aaff">r</font> <font color="#00e7ff">r</font>
<font color="#0055ff">a</font> <font color="#00e6ff">a</font>
<font color="#0000ff">i</font> <font color="#00e1ff">i</font>
<font color="#5500ff">n</font> <font color="#00d4ff">n</font>
<font color="#aa00ff">b</font> <font color="#00bdff">b</font>
<font color="#ff00ff">o</font> <font color="#9598ff">o</font>
<font color="#ff00aa">w</font> <font color="#ff60ff">w</font>
<font color="#ff0055">!</font> <font color="#ff00ff">!</font>
""".trimIndentOneLine() """.trimIndentOneLine()
assertEquals(expected, rainbowGenerator.generate("This is a rainbow!")) assertEquals(expected, rainbowGenerator.generate("This is a rainbow!"))
@ -73,19 +73,19 @@ class RainbowGeneratorTest {
@Test @Test
fun testEmoji1() { fun testEmoji1() {
assertEquals("""<font color="#ff0000">🤞</font>""", rainbowGenerator.generate("\uD83E\uDD1E")) // 🤞 assertEquals("""<font color="#ff00be">🤞</font>""", rainbowGenerator.generate("\uD83E\uDD1E")) // 🤞
} }
@Test @Test
fun testEmoji2() { fun testEmoji2() {
assertEquals("""<font color="#ff0000">🤞</font>""", rainbowGenerator.generate("🤞")) assertEquals("""<font color="#ff00be">🤞</font>""", rainbowGenerator.generate("🤞"))
} }
@Test @Test
fun testEmoji3() { fun testEmoji3() {
val expected = """ val expected = """
<font color="#ff0000">🤞</font> <font color="#ff00be">🤞</font>
<font color="#00ffff">🙂</font> <font color="#00e6b6">🙂</font>
""".trimIndentOneLine() """.trimIndentOneLine()
assertEquals(expected, rainbowGenerator.generate("🤞🙂")) assertEquals(expected, rainbowGenerator.generate("🤞🙂"))
@ -94,20 +94,20 @@ class RainbowGeneratorTest {
@Test @Test
fun testEmojiMix1() { fun testEmojiMix1() {
val expected = """ val expected = """
<font color="#ff0000">H</font> <font color="#ff00be">H</font>
<font color="#ff6d00">e</font> <font color="#ff005d">e</font>
<font color="#ffdb00">l</font> <font color="#ff6700">l</font>
<font color="#b6ff00">l</font> <font color="#ffa100">l</font>
<font color="#49ff00">o</font> <font color="#b2c400">o</font>
<font color="#00ff92">🤞</font> <font color="#00e147">🤞</font>
<font color="#0092ff">w</font> <font color="#00e7ff">w</font>
<font color="#0024ff">o</font> <font color="#00e4ff">o</font>
<font color="#4900ff">r</font> <font color="#00d6ff">r</font>
<font color="#b600ff">l</font> <font color="#00b9ff">l</font>
<font color="#ff00db">d</font> <font color="#da83ff">d</font>
<font color="#ff006d">!</font> <font color="#ff03ff">!</font>
""".trimIndentOneLine() """.trimIndentOneLine()
assertEquals(expected, rainbowGenerator.generate("Hello 🤞 world!")) assertEquals(expected, rainbowGenerator.generate("Hello 🤞 world!"))
@ -116,8 +116,8 @@ class RainbowGeneratorTest {
@Test @Test
fun testEmojiMix2() { fun testEmojiMix2() {
val expected = """ val expected = """
<font color="#ff0000">a</font> <font color="#ff00be">a</font>
<font color="#00ffff">🤞</font> <font color="#00e6b6">🤞</font>
""".trimIndentOneLine() """.trimIndentOneLine()
assertEquals(expected, rainbowGenerator.generate("a🤞")) assertEquals(expected, rainbowGenerator.generate("a🤞"))
@ -126,8 +126,8 @@ class RainbowGeneratorTest {
@Test @Test
fun testEmojiMix3() { fun testEmojiMix3() {
val expected = """ val expected = """
<font color="#ff0000">🤞</font> <font color="#ff00be">🤞</font>
<font color="#00ffff">a</font> <font color="#00e6b6">a</font>
""".trimIndentOneLine() """.trimIndentOneLine()
assertEquals(expected, rainbowGenerator.generate("🤞a")) assertEquals(expected, rainbowGenerator.generate("🤞a"))
@ -135,6 +135,6 @@ class RainbowGeneratorTest {
@Test @Test
fun testError1() { fun testError1() {
assertEquals("<font color=\"#ff0000\">\uD83E</font>", rainbowGenerator.generate("\uD83E")) assertEquals("<font color=\"#ff00be\">\uD83E</font>", rainbowGenerator.generate("\uD83E"))
} }
} }