Skip to content
Snippets Groups Projects
Commit 394de033 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Support also hex background colors

parent 6dbbc49a
Branches
Tags
No related merge requests found
......@@ -196,16 +196,32 @@ class IrcFormatDeserializer(private val context: Context) {
plainText.append(str.substring(i - normalCount, i))
normalCount = 0
val colorStart = i + 1
val colorEnd = findEndOfHexNumber(str, colorStart)
val foregroundStart = i + 1
val foregroundEnd = findEndOfHexNumber(str, foregroundStart)
// If we have a foreground element
if (colorEnd > colorStart) {
val foreground = readHexNumber(str, colorStart, colorEnd)
if (foregroundEnd > foregroundStart) {
val foreground = readHexNumber(str, foregroundStart, foregroundEnd)
var background: Int = -1
var backgroundEnd = -1
// If we have a background code, read it
if (str.length > foregroundEnd && str[foregroundEnd] == ',') {
backgroundEnd = findEndOfHexNumber(str, foregroundEnd + 1)
background = readHexNumber(str, foregroundEnd + 1, backgroundEnd)
}
// If previous element was also a color element, try to reuse background
if (hexColor != null) {
// Apply old format
if (colorize) hexColor.apply(plainText, plainText.length)
// Reuse old background, if possible
if (background == -1)
background = hexColor.format.background
}
// Add new format
hexColor = FormatDescription(plainText.length, HexIrcFormat(foreground))
hexColor = FormatDescription(plainText.length, HexIrcFormat(foreground, background))
// i points in front of the next character
i = colorEnd - 1
i = (if (backgroundEnd == -1) foregroundEnd else backgroundEnd) - 1
// Otherwise assume this is a closing tag
} else if (hexColor != null) {
......@@ -326,14 +342,22 @@ class IrcFormatDeserializer(private val context: Context) {
}
}
private inner class HexIrcFormat(val color: Int) : IrcFormat {
private inner class HexIrcFormat(val foreground: Int, val background: Int) : IrcFormat {
override fun applyTo(editable: SpannableStringBuilder, from: Int, to: Int) {
if (foreground >= 0) {
editable.setSpan(
IrcHexColorSpan(color or 0xFFFFFF.inv()), from, to,
IrcHexForegroundColorSpan(foreground or 0xFFFFFF.inv()), from, to,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
}
if (background >= 0) {
editable.setSpan(
IrcHexBackgroundColorSpan(background or 0xFFFFFF.inv()), from, to,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
}
}
}
private inner class ColorIrcFormat(val foreground: Byte, val background: Byte) : IrcFormat {
......
package de.kuschku.quasseldroid_ng.util.irc.format.spans
import android.support.annotation.ColorInt
import android.text.style.BackgroundColorSpan
class IrcHexBackgroundColorSpan(@ColorInt color: Int) : BackgroundColorSpan(color),
Copyable<IrcHexBackgroundColorSpan> {
override fun copy() = IrcHexBackgroundColorSpan(backgroundColor)
}
......@@ -3,7 +3,7 @@ package de.kuschku.quasseldroid_ng.util.irc.format.spans
import android.support.annotation.ColorInt
import android.text.style.ForegroundColorSpan
class IrcHexColorSpan(@ColorInt color: Int) : ForegroundColorSpan(color),
Copyable<IrcHexColorSpan> {
override fun copy() = IrcHexColorSpan(foregroundColor)
class IrcHexForegroundColorSpan(@ColorInt color: Int) : ForegroundColorSpan(color),
Copyable<IrcHexForegroundColorSpan> {
override fun copy() = IrcHexForegroundColorSpan(foregroundColor)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment