Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions PlatCurses.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<short>::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<short>(i);
init_color(c, color.GetRed() * 1000.0 / 255, color.GetGreen() * 1000.0 / 255,
color.GetBlue() * 1000.0 / 255);
colors.emplace(color.OpaqueRGB(), c);
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions PlatCurses.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class Colors {
std::map<int, short> colors; // map of RGB ints to curses color numbers.
std::map<std::pair<short, short>, short> pairs; // map of curses colors to their pair numbers
int colorOffset = 0, pairOffset = 0;
bool usePalette = true;

Colors();
static Colors &instance();
Expand All @@ -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);
};
Expand Down
2 changes: 2 additions & 0 deletions ScintillaCurses.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ void scintilla_update_cursor(void *sci) {

void scintilla_delete(void *sci) { delete reinterpret_cast<ScintillaCurses *>(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);
}
Expand Down
6 changes: 6 additions & 0 deletions ScintillaCurses.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ Parameters:

Returns: `void`

<a id="scintilla_disable_color_palette"></a>
### `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()`.

<a id="scintilla_get_clipboard"></a>
### `scintilla_get_clipboard`(*sci*, *len*)

Expand Down
4 changes: 4 additions & 0 deletions docs/scinterm.luadoc
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand Down