about summary refs log tree commit diff stats
path: root/src/types
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-24 18:14:00 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-24 18:19:58 +0100
commitf11c942585446be074412b2cc270f30532351882 (patch)
tree5bc37a67f6bde5dccd8d685b47ef6f9602e33a26 /src/types
parentea3c192626b4a33d9a6201d82a1b4a1f306f58eb (diff)
downloadchawan-f11c942585446be074412b2cc270f30532351882.tar.gz
Allow non-RGB colors in CSS
The -cha-ansi color type now sets ANSI colors in CSS.

Also, color correction etc. has been improved a bit:

* don't completely reset output state in processFormat for new colors
* defaultColor is now separated from ANSI color type 0
* bright ANSI colors are no longer replaced with bold + dark variant
* replaced ANSI color map to match xterm defaults
Diffstat (limited to 'src/types')
-rw-r--r--src/types/cell.nim11
-rw-r--r--src/types/color.nim36
2 files changed, 27 insertions, 20 deletions
diff --git a/src/types/cell.nim b/src/types/cell.nim
index f3ffd919..2f2e5203 100644
--- a/src/types/cell.nim
+++ b/src/types/cell.nim
@@ -204,11 +204,8 @@ proc parseSGRDefColor(parser: AnsiCodeParser, format: var Format,
       set_color cellColor(gray(param0))
   elif u == 5:
     let param0 = parser.getParamU8(i, colon = true)
-    if param0 in 0u8..7u8:
+    if param0 in 0u8..15u8:
       set_color cellColor(ANSIColor(param0))
-    elif param0 in 8u8..15u8:
-      format.bold = true
-      set_color cellColor(ANSIColor(param0 - 8))
     elif param0 in 16u8..255u8:
       set_color cellColor(EightBitColor(param0))
   else:
@@ -229,11 +226,9 @@ proc parseSGRColor(parser: AnsiCodeParser, format: var Format,
   elif u == 49:
     format.bgcolor = defaultColor
   elif u in 90u8..97u8:
-    format.fgcolor = cellColor(ANSIColor(u - 90u8))
-    format.bold = true
+    format.fgcolor = cellColor(ANSIColor(u - 82))
   elif u in 100u8..107u8:
-    format.bgcolor = cellColor(ANSIColor(u - 90u8))
-    format.bold = true
+    format.bgcolor = cellColor(ANSIColor(u - 92))
   else:
     return false
   return true
diff --git a/src/types/color.nim b/src/types/color.nim
index 1a93792d..2f3f7310 100644
--- a/src/types/color.nim
+++ b/src/types/color.nim
@@ -19,11 +19,17 @@ type
 
   EightBitColor* = distinct uint8
 
+  # ctNone: default color (intentionally 0), n is unused
+  # ctANSI: ANSI color, as selected by SGR 38/48
+  # ctRGB: RGB color
+  ColorTag* {.size: sizeof(uint32).} = enum
+    ctNone, ctANSI, ctRGB
+
   CellColor* = object
-    rgb*: bool
+    t*: ColorTag
     n: uint32
 
-converter toRGBColor*(i: RGBAColor): RGBColor =
+func toRGBColor*(i: RGBAColor): RGBColor =
   return RGBColor(uint32(i) and 0xFFFFFFu32)
 
 converter toRGBAColor*(i: RGBColor): RGBAColor =
@@ -36,6 +42,9 @@ func `==`*(a, b: ANSIColor): bool {.borrow.}
 func rgbcolor*(color: CellColor): RGBColor =
   cast[RGBColor](color.n)
 
+func rgbacolor*(color: CellColor): RGBAColor =
+  cast[RGBAColor](color.n)
+
 func color*(color: CellColor): uint8 =
   cast[uint8](color.n)
 
@@ -43,17 +52,20 @@ func eightbit*(color: CellColor): EightBitColor =
   EightBitColor(color.color)
 
 func cellColor*(rgb: RGBColor): CellColor =
-  return CellColor(rgb: true, n: uint32(rgb))
+  return CellColor(t: ctRGB, n: uint32(rgb) or 0xFF000000u32)
 
-func cellColor*(c: ANSIColor): CellColor =
-  return CellColor(rgb: false, n: uint32(c) mod 10)
+func cellColor*(rgba: RGBAColor): CellColor =
+  return CellColor(t: ctRGB, n: uint32(rgba))
 
-#TODO maybe bright ANSI colors? (8..15)
+#TODO bright ANSI colors (8..15)
+
+func cellColor*(c: ANSIColor): CellColor =
+  return CellColor(t: ctANSI, n: uint32(c))
 
 func cellColor*(c: EightBitColor): CellColor =
-  return CellColor(rgb: false, n: uint32(c))
+  return CellColor(t: ctANSI, n: uint32(c))
 
-const defaultColor* = CellColor(rgb: false, n: 0)
+const defaultColor* = CellColor(t: ctNone, n: 0)
 
 const
   ANSI_BLACK* = ANSIColor(0u8)
@@ -374,7 +386,7 @@ func gray*(n: uint8): RGBColor =
   return rgb(n, n, n) #TODO use yuv instead?
 
 # NOTE: this assumes n notin 0..15 (which would be ANSI 4-bit)
-func eightBitToRGB*(param0: EightBitColor): RGBColor =
+func toRGB*(param0: EightBitColor): RGBColor =
   doAssert uint8(param0) notin 0u8..15u8
   let u = uint8(param0)
   if u in 16u8..231u8:
@@ -389,7 +401,7 @@ func eightBitToRGB*(param0: EightBitColor): RGBColor =
     let n = (u - 232) * 10 + 8
     return gray(n)
 
-func rgbToEightBit*(rgb: RGBColor): EightBitColor =
+func toEightBit*(rgb: RGBColor): EightBitColor =
   let r = int(rgb.r)
   let g = int(rgb.g)
   let b = int(rgb.b)
@@ -410,8 +422,8 @@ template `$`*(rgbcolor: RGBColor): string =
   "rgb(" & $rgbcolor.r & ", " & $rgbcolor.g & ", " & $rgbcolor.b & ")"
 
 template `$`*(color: CellColor): string =
-  if color.rgb:
-    $color.rgbcolor
+  if color.t == ctRGB:
+    $color.rgbacolor
   else:
     "tcolor" & $color.n