diff --git a/app/src/main/java/de/kuschku/quasseldroid/util/irc/format/IrcFormatSerializer.kt b/app/src/main/java/de/kuschku/quasseldroid/util/irc/format/IrcFormatSerializer.kt
index f554edfcb1ccd8a99e0e3b7a187cc110aef21cb9..1116e492564a2d7b715d89135c3483b3a894ea84 100644
--- a/app/src/main/java/de/kuschku/quasseldroid/util/irc/format/IrcFormatSerializer.kt
+++ b/app/src/main/java/de/kuschku/quasseldroid/util/irc/format/IrcFormatSerializer.kt
@@ -181,16 +181,28 @@ class IrcFormatSerializer @Inject constructor(private val context: Context) {
         val afterForegroundCode = mircColorMap[afterForeground]
         val afterBackgroundCode = mircColorMap[afterBackground]
 
-        if (afterForegroundCode != null || afterBackgroundCode != null) {
+        val hasForegroundBefore = foreground != null
+        val hasBackgroundBefore = background != null
+        val foregroundBeforeCodeValid = foregroundCode != null
+        val backgroundBeforeCodeValid = backgroundCode != null
+
+        val hasForegroundAfter = afterForeground != null
+        val hasBackgroundAfter = afterBackground != null
+        val foregroundAfterCodeValid = afterForegroundCode != null
+        val backgroundAfterCodeValid = afterBackgroundCode != null
+
+        if ((!hasBackgroundAfter || backgroundAfterCodeValid) &&
+            (!hasForegroundAfter || foregroundAfterCodeValid)) {
           if (afterForegroundCode == backgroundCode && afterBackgroundCode == foregroundCode) {
             writeSwap(afterForegroundCode, afterBackgroundCode)
           } else {
             writeColor(afterForegroundCode, afterBackgroundCode)
           }
-        } else if (afterForeground != null || afterBackground != null) {
+        } else if (hasForegroundAfter || hasBackgroundAfter) {
           writeHexColor(afterForeground, afterBackground)
         } else {
-          if (foregroundCode != null || backgroundCode != null) {
+          if ((!hasBackgroundBefore || backgroundBeforeCodeValid) &&
+              (!hasForegroundBefore || foregroundBeforeCodeValid)) {
             writeColor(afterForeground, afterBackground)
           } else {
             writeHexColor(afterForeground, afterBackground)