From 86c6530e4643ed0d9044ae682b8ebcac33bab0a3 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 14 Nov 2023 20:40:48 +0100 Subject: [PATCH] palette: more accurate color conversion from 8 to 4 bits channels set_rgb8(), color8to4(), channel8to4() --- compiler/res/prog8lib/cx16/palette.p8 | 27 +++++++++++++++++++-------- docs/source/todo.rst | 1 - 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/compiler/res/prog8lib/cx16/palette.p8 b/compiler/res/prog8lib/cx16/palette.p8 index ee49129d2..33c9174dd 100644 --- a/compiler/res/prog8lib/cx16/palette.p8 +++ b/compiler/res/prog8lib/cx16/palette.p8 @@ -42,19 +42,30 @@ palette { ubyte red ubyte greenblue repeat num_colors { - red = @(palette_bytes_ptr) >> 4 - palette_bytes_ptr++ - greenblue = @(palette_bytes_ptr) & %11110000 - palette_bytes_ptr++ - greenblue |= @(palette_bytes_ptr) >> 4 ; add Blue - palette_bytes_ptr++ - cx16.vpoke(1, vera_palette_ptr, greenblue) + cx16.r1 = color8to4(palette_bytes_ptr) + palette_bytes_ptr+=3 + cx16.vpoke(1, vera_palette_ptr, cx16.r1H) ; $GB vera_palette_ptr++ - cx16.vpoke(1, vera_palette_ptr, red) + cx16.vpoke(1, vera_palette_ptr, cx16.r1L) ; $0R vera_palette_ptr++ } } + sub color8to4(uword colorpointer) -> uword { + ; accurately convert 24 bits (3 bytes) RGB color, in that order in memory, to 16 bits $GB;$0R colorvalue + cx16.r1 = colorpointer + cx16.r0 = channel8to4(@(cx16.r1)) ; (red) -> $00:0R + cx16.r0H = channel8to4(@(cx16.r1+1))<<4 ; (green) -> $G0:0R + cx16.r0H |= channel8to4(@(cx16.r1+2)) ; (blue) -> $GB:0R + return cx16.r0 + } + + sub channel8to4(ubyte channelvalue) -> ubyte { + ; accurately convert a single 8 bit color channel value to 4 bits, see https://threadlocalmutex.com/?p=48 + return msb(channelvalue * $000f + 135) + } + + sub set_monochrome(uword screencolorRGB, uword drawcolorRGB) { vera_palette_ptr = $fa00 cx16.vpoke(1, vera_palette_ptr, lsb(screencolorRGB)) ; G,B diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 39475ae6d..809323916 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,7 +2,6 @@ TODO ==== -- palette: mnore accurate conversion to 4 bit colorspage in set_rgb8, by doing (x * 15 + 135) >> 8, see https://threadlocalmutex.com/?p=48 - fix the compiler crash on s1[n+1] = s1[2] where s1 is a subroutine param (regular variable works) - improve the working of %option merge: should be able to merge your own stuff into say textio. , and improve the docs about it too. - give error when using %option merge in module scope.