-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also update rrtop sample with several color palettes
- Loading branch information
Showing
27 changed files
with
1,307 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 69 additions & 2 deletions
71
mosaic-runtime/src/commonMain/kotlin/com/jakewharton/mosaic/ansi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,71 @@ | ||
package com.jakewharton.mosaic | ||
|
||
internal const val clearLine = "\u001B[K" | ||
internal const val cursorUp = "\u001B[F" | ||
import com.github.ajalt.mordant.rendering.AnsiLevel as MordantAnsiLevel | ||
import com.jakewharton.mosaic.ui.AnsiLevel | ||
import com.jakewharton.mosaic.ui.Color | ||
import kotlin.math.roundToInt | ||
|
||
private const val ESC = "\u001B" | ||
internal const val CSI = "$ESC[" | ||
|
||
internal const val ansiReset = "${CSI}0" | ||
internal const val clearLine = "${CSI}K" | ||
internal const val cursorUp = "${CSI}F" | ||
|
||
internal const val ansiSeparator = ";" | ||
internal const val ansiClosingCharacter = "m" | ||
|
||
internal const val ansiFgColorSelector = 38 | ||
internal const val ansiFgColorReset = 39 | ||
internal const val ansiFgColorOffset = 0 | ||
|
||
internal const val ansiBgColorSelector = 48 | ||
internal const val ansiBgColorReset = 49 | ||
internal const val ansiBgColorOffset = 10 | ||
|
||
internal const val ansiSelectorColor256 = 5 | ||
internal const val ansiSelectorColorRgb = 2 | ||
|
||
internal fun MordantAnsiLevel.toMosaicAnsiLevel(): AnsiLevel { | ||
return when (this) { | ||
MordantAnsiLevel.NONE -> AnsiLevel.NONE | ||
MordantAnsiLevel.ANSI16 -> AnsiLevel.ANSI16 | ||
MordantAnsiLevel.ANSI256 -> AnsiLevel.ANSI256 | ||
MordantAnsiLevel.TRUECOLOR -> AnsiLevel.TRUECOLOR | ||
} | ||
} | ||
|
||
// simpler version without full conversion to HSV | ||
// https://github.com/ajalt/colormath/blob/4a0cc9796c743cb4965407204ee63b40aaf22fca/colormath/src/commonMain/kotlin/com/github/ajalt/colormath/model/RGB.kt#L301 | ||
internal fun Color.toAnsi16Code(): Int { | ||
val value = (maxOf(redFloat, greenFloat, blueFloat) * 100).roundToInt() | ||
if (value == 30) { | ||
return 30 | ||
} | ||
val v = value / 50 | ||
val ansiCode = 30 + ( | ||
(blueFloat.roundToInt() * 4) | ||
or (greenFloat.roundToInt() * 2) | ||
or redFloat.roundToInt() | ||
) | ||
return if (v == 2) ansiCode + 60 else ansiCode | ||
} | ||
|
||
// https://github.com/ajalt/colormath/blob/4a0cc9796c743cb4965407204ee63b40aaf22fca/colormath/src/commonMain/kotlin/com/github/ajalt/colormath/model/RGB.kt#L310 | ||
internal fun Color.toAnsi256Code(): Int { | ||
val ri = redInt | ||
val gi = greenInt | ||
val bi = blueInt | ||
// grayscale | ||
return if (ri == gi && gi == bi) { | ||
when { | ||
ri < 8 -> 16 | ||
ri > 248 -> 231 | ||
else -> (((ri - 8) / 247.0) * 24.0).roundToInt() + 232 | ||
} | ||
} else { | ||
16 + (36 * (redFloat * 5).roundToInt()) + | ||
(6 * (greenFloat * 5).roundToInt()) + | ||
(blueFloat * 5).roundToInt() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.