diff --git a/PlatCurses.cxx b/PlatCurses.cxx index dd995fa..c311ac1 100644 --- a/PlatCurses.cxx +++ b/PlatCurses.cxx @@ -131,8 +131,10 @@ Colors &Colors::instance() { short Colors::get(const ColourRGBA &color) { if (const auto entry = colors.find(color.OpaqueRGB()); entry != colors.end()) return entry->second; - if (colorOffset + colors.size() >= std::numeric_limits::max()) return COLOR_WHITE; - const short c = colorOffset + colors.size(); + size_t i = colorOffset + colors.size(); + if (!usePalette) i += std::clamp(COLORS, 8, 16); // do not overwrite a default color + if (i >= COLORS) return COLOR_WHITE; + const auto c = static_cast(i); init_color(c, color.GetRed() * 1000.0 / 255, color.GetGreen() * 1000.0 / 255, color.GetBlue() * 1000.0 / 255); colors.emplace(color.OpaqueRGB(), c); @@ -160,6 +162,12 @@ ColourRGBA Colors::Find(const short color) { return Colors::White; } +void Colors::DisablePalette() { + instance().usePalette = false; + instance().colors.clear(); + instance().pairs.clear(); +} + void Colors::SetOffsets(int colorOffset, int pairOffset) { instance().colorOffset = colorOffset; instance().pairOffset = pairOffset; diff --git a/PlatCurses.h b/PlatCurses.h index 93fe715..a00d3e3 100644 --- a/PlatCurses.h +++ b/PlatCurses.h @@ -141,6 +141,7 @@ class Colors { std::map colors; // map of RGB ints to curses color numbers. std::map, short> pairs; // map of curses colors to their pair numbers int colorOffset = 0, pairOffset = 0; + bool usePalette = true; Colors(); static Colors &instance(); @@ -156,6 +157,8 @@ class Colors { /** Returns the Scintilla color for a given curses color number. */ static ColourRGBA Find(const short color); + /** Disables use of the terminal's default color palette. */ + static void DisablePalette(); /** Sets the offsets for colors and color pairs generated on-demand. */ static void SetOffsets(int colorOffset, int pairOffset); }; diff --git a/ScintillaCurses.cxx b/ScintillaCurses.cxx index 9cd10d7..597fef9 100644 --- a/ScintillaCurses.cxx +++ b/ScintillaCurses.cxx @@ -699,6 +699,8 @@ void scintilla_update_cursor(void *sci) { void scintilla_delete(void *sci) { delete reinterpret_cast(sci); } +void scintilla_disable_color_palette() { Scintilla::Internal::Colors::DisablePalette(); } + void scintilla_set_color_offsets(int color_offset, int pair_offset) { Scintilla::Internal::Colors::SetOffsets(color_offset, pair_offset); } diff --git a/ScintillaCurses.h b/ScintillaCurses.h index 159a0f5..d9e5325 100644 --- a/ScintillaCurses.h +++ b/ScintillaCurses.h @@ -116,6 +116,12 @@ void scintilla_update_cursor(void *sci); */ void scintilla_delete(void *sci); +/** + * Disables use of the terminal's default color palette for all Scintilla windows. + * This only needs to be called once, and ideally before any calls to `scintilla_new()`. + */ +void scintilla_disable_color_palette(void); + /** * Sets the offsets for colors and color pairs generated on-demand. * Applications that define their own colors and color pairs can tell Scinterm where to start from. diff --git a/docs/README.md b/docs/README.md index 300961d..8014e19 100644 --- a/docs/README.md +++ b/docs/README.md @@ -67,6 +67,8 @@ application. ### Colors If your terminal emulator supports RGB colors, you may use them freely in Scintilla messages. +However, if you use any of the colors listed below, they will be mapped to your terminal's +default color palette. To prevent this, call [`scintilla_disable_color_palette()`][]. If you prefer to use your terminal emulator's palette of up to 16 colors, you must use the colors from the following table, which are listed in Scintilla's "0xBBGGRR" format. @@ -82,6 +84,8 @@ colors from the following table, which are listed in Scintilla's "0xBBGGRR" form Your terminal will map these colors to its palette for display. In some terminals, you may need to set a style's bold attribute in order to use the light color variant. +[`scintilla_disable_color_palette`]: api.md#scintilla_disable_color_palette + ## Curses Compatibility Scinterm lacks some Scintilla features due to the terminal's constraints: diff --git a/docs/api.md b/docs/api.md index 659233b..5be6611 100644 --- a/docs/api.md +++ b/docs/api.md @@ -16,6 +16,13 @@ Parameters: Returns: `void` + +### `scintilla_disable_color_palette`() + +Disables use of the terminal's default color palette for all Scintilla windows. + +This only needs to be called once, and ideally before any calls to `scintilla_new()`. + ### `scintilla_get_clipboard`(*sci*, *len*) diff --git a/docs/scinterm.luadoc b/docs/scinterm.luadoc index f96f00c..ab58038 100644 --- a/docs/scinterm.luadoc +++ b/docs/scinterm.luadoc @@ -88,6 +88,10 @@ -- @return `void` -- @function scintilla_delete +--- Disables use of the terminal's default color palette for all Scintilla windows. +-- This only needs to be called once, and ideally before any calls to `scintilla_new()`. +-- @function scintilla_disable_color_palette + --- Sets the offsets for colors and color pairs generated on-demand. -- Applications that define their own colors and color pairs can tell Scinterm where to start from. -- @param color_offset The offset for Scinterm's calls to `init_color()`.