palette256 -> ncpalette #1570 (#1573)

everything prefixed with palette256 is now prefixed with ncpalette
This commit is contained in:
Nick Black 2021-04-22 04:11:06 -04:00 committed by GitHub
parent 5e6862f34c
commit c8680255be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 112 additions and 71 deletions

View File

@ -4,6 +4,10 @@ rearrangements of Notcurses.
* 2.2.9 (not yet released)
* Added two new stats, `sprixelemissions` and `sprixelelisions`.
* Added `notcurses_canhalfblock()` and `notcurses_canquadrant()`.
* The `palette256` type has been renamed `ncpalette`, and all functions
prefixed with `palette256_` have been deprecated in favor of versions
prefixed with `ncpalette_`, which the former now wrap. The old versions
will be removed in ABI3.
* 2.2.8 (2021-04-18)
* All remaining functions prefixed with `cell_` or `cells_` have been

View File

@ -190,13 +190,13 @@ int nccell_load(struct ncplane* n, cell* c, const char* gcluster);
int nccell_duplicate(struct ncplane* n, cell* targ, const cell* c);
void nccell_release(struct ncplane* n, cell* c);
const char* nccell_extended_gcluster(const struct ncplane* n, const cell* c);
typedef struct palette256 {
typedef struct ncpalette {
// We store the RGB values as a regular ol' channel
uint32_t chans[256];
} palette256;
palette256* palette256_new(struct notcurses* nc);
int palette256_use(struct notcurses* nc, const palette256* p);
void palette256_free(palette256* p);
} ncpalette;
ncpalette* ncpalette_new(struct notcurses* nc);
int ncpalette_use(struct notcurses* nc, const ncpalette* p);
void ncpalette_free(ncpalette* p);
typedef enum {
NCSCALE_NONE,
NCSCALE_SCALE,

View File

@ -11,25 +11,25 @@ notcurses_palette - operations on notcurses palettes
**#include <notcurses/notcurses.h>**
```c
typedef struct palette256 {
typedef struct ncpalette {
// We store the RGB values as a regular ol' channel
uint32_t chans[256];
} palette256;
} ncpalette;
```
**bool notcurses_cantruecolor(const struct notcurses* ***nc***);**
**palette256* palette256_new(struct notcurses* ***nc***);**
**ncpalette* ncpalette_new(struct notcurses* ***nc***);**
**int palette256_use(struct notcurses* ***nc***, const palette256* ***p***);**
**int ncpalette_use(struct notcurses* ***nc***, const ncpalette* ***p***);**
**int palette256_set_rgb8(palette256* ***p***, int ***idx***, int ***r***, int ***g***, int ***b***);**
**int ncpalette_set_rgb8(ncpalette* ***p***, int ***idx***, int ***r***, int ***g***, int ***b***);**
**int palette256_set(palette256* ***p***, int ***idx***, unsigned ***rgb***);**
**int ncpalette_set(ncpalette* ***p***, int ***idx***, unsigned ***rgb***);**
**int palette256_get_rgb8(const palette256* ***p***, int ***idx***, int* restrict ***r***, int* restrict ***g***, int* restrict ***b***);**
**int ncpalette_get_rgb8(const ncpalette* ***p***, int ***idx***, int* restrict ***r***, int* restrict ***g***, int* restrict ***b***);**
**void palette256_free(palette256* ***p***);**
**void ncpalette_free(ncpalette* ***p***);**
**bool notcurses_canchangecolors(const struct notcurses* ***nc***);**
@ -45,9 +45,9 @@ since a single command can affect many cells on the screen.
# RETURN VALUES
Functions returning `int` return -1 on failure, or 0 on success. Failure is
always due to invalid inputs. Functions returning `bool` are predicates, and
return the requested value. Functions returning `unsigned` forms return the
Functions returning **int** return -1 on failure, or 0 on success. Failure is
always due to invalid inputs. Functions returning **bool** are predicates, and
return the requested value. Functions returning **unsigned** forms return the
input, modified as requested.
# SEE ALSO

View File

@ -11,7 +11,7 @@
#include "CellStyle.hh"
#include "NCKey.hh"
#include "NCLogLevel.hh"
#include "Palette256.hh"
#include "Palette.hh"
#include "Plane.hh"
#include "Root.hh"
#include "_helpers.hh"
@ -182,7 +182,7 @@ namespace ncpp
notcurses_stats_reset (nc, stats);
}
bool use (const Palette256 *p) const
bool use (const Palette *p) const
{
if (p == nullptr)
throw invalid_argument ("'p' must be a valid pointer");
@ -190,9 +190,9 @@ namespace ncpp
return use (*p);
}
bool use (const Palette256 &p) const NOEXCEPT_MAYBE
bool use (const Palette &p) const NOEXCEPT_MAYBE
{
return error_guard (palette256_use (nc, reinterpret_cast<const palette256*>(&p)), -1);
return error_guard (ncpalette_use (nc, reinterpret_cast<const ncpalette*>(&p)), -1);
}
bool render () const NOEXCEPT_MAYBE

View File

@ -1,5 +1,5 @@
#ifndef __NCPP_PALETTE256_HH
#define __NCPP_PALETTE256_HH
#ifndef __NCPP_PALETTE_HH
#define __NCPP_PALETTE_HH
#include "Root.hh"
#include "_helpers.hh"
@ -8,40 +8,40 @@ namespace ncpp
{
class NotCurses;
class NCPP_API_EXPORT Palette256 : public Root
class NCPP_API_EXPORT Palette : public Root
{
public:
Palette256 (NotCurses *ncinst = nullptr)
Palette (NotCurses *ncinst = nullptr)
: Root (ncinst)
{
palette = palette256_new (get_notcurses ());
palette = ncpalette_new (get_notcurses ());
if (palette == nullptr)
throw init_error ("Notcurses failed to create a new palette");
}
~Palette256 ()
~Palette ()
{
palette256_free (palette);
ncpalette_free (palette);
}
operator palette256* () noexcept
operator ncpalette* () noexcept
{
return palette;
}
operator palette256 const* () const noexcept
operator ncpalette const* () const noexcept
{
return palette;
}
bool set (int idx, int r, int g, int b) const NOEXCEPT_MAYBE
{
return error_guard (palette256_set_rgb8 (palette, idx, r, g, b), -1);
return error_guard (ncpalette_set_rgb8 (palette, idx, r, g, b), -1);
}
bool set (int idx, unsigned rgb) const NOEXCEPT_MAYBE
{
return error_guard (palette256_set (palette, idx, rgb), -1);
return error_guard (ncpalette_set (palette, idx, rgb), -1);
}
bool get (int idx, unsigned *r, unsigned *g, unsigned *b) const
@ -58,11 +58,11 @@ namespace ncpp
bool get (int idx, unsigned &r, unsigned &g, unsigned &b) const NOEXCEPT_MAYBE
{
return error_guard (palette256_get_rgb8 (palette, idx, &r, &g, &b), -1);
return error_guard (ncpalette_get_rgb8 (palette, idx, &r, &g, &b), -1);
}
private:
palette256 *palette;
ncpalette *palette;
};
}
#endif

View File

@ -631,8 +631,6 @@ typedef struct nccell {
uint64_t channels; // + 8B == 16B
} nccell;
typedef nccell cell; // FIXME backwards-compat, remove in ABI3
#define CELL_TRIVIAL_INITIALIZER { .gcluster = 0, .gcluster_backstop = 0, .width = 0, .stylemask = 0, .channels = 0, }
// do *not* load invalid EGCs using these macros! there is no way for us to
// protect against such misuse here. problems *will* ensue. similarly, do not
@ -2863,22 +2861,22 @@ API int notcurses_cursor_disable(struct notcurses* nc);
// performant to use indexed colors, since it's much less data to write to the
// terminal. If you can limit yourself to 256 colors, that's probably best.
typedef struct palette256 {
typedef struct ncpalette {
uint32_t chans[NCPALETTESIZE]; // RGB values as regular ol' channels
} palette256;
} ncpalette;
// Create a new palette store. It will be initialized with notcurses' best
// knowledge of the currently configured palette. The palette upon startup
// cannot be reliably detected, sadly.
API ALLOC palette256* palette256_new(struct notcurses* nc);
API ALLOC ncpalette* ncpalette_new(struct notcurses* nc);
// Attempt to configure the terminal with the provided palette 'p'. Does not
// transfer ownership of 'p'; palette256_free() can (ought) still be called.
API int palette256_use(struct notcurses* nc, const palette256* p);
API int ncpalette_use(struct notcurses* nc, const ncpalette* p);
// Manipulate entries in the palette store 'p'. These are *not* locked.
static inline int
palette256_set_rgb8(palette256* p, int idx, int r, int g, int b){
ncpalette_set_rgb8(ncpalette* p, int idx, int r, int g, int b){
if(idx < 0 || (size_t)idx > sizeof(p->chans) / sizeof(*p->chans)){
return -1;
}
@ -2886,7 +2884,7 @@ palette256_set_rgb8(palette256* p, int idx, int r, int g, int b){
}
static inline int
palette256_set(palette256* p, int idx, unsigned rgb){
ncpalette_set(ncpalette* p, int idx, unsigned rgb){
if(idx < 0 || (size_t)idx > sizeof(p->chans) / sizeof(*p->chans)){
return -1;
}
@ -2894,7 +2892,7 @@ palette256_set(palette256* p, int idx, unsigned rgb){
}
static inline int
palette256_get_rgb8(const palette256* p, int idx, unsigned* RESTRICT r, unsigned* RESTRICT g, unsigned* RESTRICT b){
ncpalette_get_rgb8(const ncpalette* p, int idx, unsigned* RESTRICT r, unsigned* RESTRICT g, unsigned* RESTRICT b){
if(idx < 0 || (size_t)idx > sizeof(p->chans) / sizeof(*p->chans)){
return -1;
}
@ -2902,7 +2900,7 @@ palette256_get_rgb8(const palette256* p, int idx, unsigned* RESTRICT r, unsigned
}
// Free the palette store 'p'.
API void palette256_free(palette256* p);
API void ncpalette_free(ncpalette* p);
// Convert the plane's content to greyscale.
API void ncplane_greyscale(struct ncplane* n);
@ -3953,6 +3951,33 @@ ncvisual_geom(const struct notcurses* nc, const struct ncvisual* n,
API struct ncplane* nctablet_ncplane(struct nctablet* t)
__attribute__ ((deprecated));
API ALLOC ncpalette* palette256_new(struct notcurses* nc)
__attribute__ ((deprecated));
API int palette256_use(struct notcurses* nc, const ncpalette* p)
__attribute__ ((deprecated));
__attribute__ ((deprecated)) static inline int
palette256_set_rgb8(ncpalette* p, int idx, int r, int g, int b){
return ncpalette_set_rgb8(p, idx, r, g, b);
}
__attribute__ ((deprecated)) static inline int
palette256_set(ncpalette* p, int idx, unsigned rgb){
return ncpalette_set(p, idx, rgb);
}
__attribute__ ((deprecated)) static inline int
palette256_get_rgb8(const ncpalette* p, int idx, unsigned* RESTRICT r, unsigned* RESTRICT g, unsigned* RESTRICT b){
return ncpalette_get_rgb8(p, idx, r, g, b);
}
API void palette256_free(ncpalette* p) __attribute__ ((deprecated));
typedef ncpalette palette256;
typedef nccell cell; // FIXME backwards-compat, remove in ABI3
#undef ALLOC
#undef API

View File

@ -26492,31 +26492,31 @@ const size_t ORIGWIDTH = 640;
// unsigned char, which must be exactly 769 bytes (256 entries * 24bpp + 1).
// this last byte ought indeed be a zero (for checking), but zeros may occur
// earlier (unlike a proper c string).
static palette256*
static ncpalette*
load_palette(struct notcurses* nc, const unsigned char* pal, size_t size){
if(size != NCPALETTESIZE * 3 + 1){
return NULL;
}
palette256* p256 = palette256_new(nc);
ncpalette* p256 = ncpalette_new(nc);
for(int idx = 0 ; idx < NCPALETTESIZE ; ++idx){
if(palette256_set_rgb8(p256, idx, pal[idx * 3], pal[idx * 3 + 1], pal[idx * 3 + 2])){
palette256_free(p256);
if(ncpalette_set_rgb8(p256, idx, pal[idx * 3], pal[idx * 3 + 1], pal[idx * 3 + 2])){
ncpalette_free(p256);
return NULL;
}
}
if(pal[NCPALETTESIZE * 3] != '\0'){
palette256_free(p256);
ncpalette_free(p256);
return NULL;
}
if(palette256_use(nc, p256)){
palette256_free(p256);
if(ncpalette_use(nc, p256)){
ncpalette_free(p256);
return NULL;
}
return p256;
}
static int
cycle_palettes(struct notcurses* nc, palette256* p){
cycle_palettes(struct notcurses* nc, ncpalette* p){
// these ranges [lower, upper] are cycling amongst themselves
static const struct {
int l, u;
@ -26539,16 +26539,16 @@ cycle_palettes(struct notcurses* nc, palette256* p){
for(s = sets ; s->l ; ++s){
unsigned tr, tg, tb;
// first grab the top rgbs (u), for use in bottom (l)
if(palette256_get_rgb8(p, s->u, &tr, &tg, &tb) < 0){
if(ncpalette_get_rgb8(p, s->u, &tr, &tg, &tb) < 0){
return -1;
}
// shift each range up one
for(int i = s->l ; i <= s->u ; ++i){
unsigned r, g, b;
if(palette256_get_rgb8(p, i, &r, &g, &b) < 0){
if(ncpalette_get_rgb8(p, i, &r, &g, &b) < 0){
return -1;
}
if(palette256_set_rgb8(p, i, tr, tg, tb)){
if(ncpalette_set_rgb8(p, i, tr, tg, tb)){
return -1;
}
tr = r;
@ -26556,7 +26556,7 @@ cycle_palettes(struct notcurses* nc, palette256* p){
tb = b;
}
}
if(palette256_use(nc, p)){
if(ncpalette_use(nc, p)){
return -1;
}
return 0;
@ -26574,7 +26574,7 @@ int jungle_demo(struct notcurses* nc){
struct timespec start, now;
clock_gettime(CLOCK_MONOTONIC, &start);
size_t have = 0, out = 0;
palette256* pal;
ncpalette* pal;
if((pal = load_palette(nc, palette, sizeof(palette))) == NULL){
return -1;
}
@ -26657,7 +26657,7 @@ int jungle_demo(struct notcurses* nc){
clock_gettime(CLOCK_MONOTONIC, &now);
nsrunning = timespec_to_ns(&now) - timespec_to_ns(&start);
}while(nsrunning > 0 && (uint64_t)nsrunning < 5 * timespec_to_ns(&demodelay));
palette256_free(pal);
ncpalette_free(pal);
return 0;
}
#endif

View File

@ -2072,14 +2072,14 @@ bool notcurses_canchangecolor(const notcurses* nc){
if(!nc->tcache.CCCflag){
return false;
}
palette256* p;
ncpalette* p;
if((unsigned)nc->tcache.colors < sizeof(p->chans) / sizeof(*p->chans)){
return false;
}
return true;
}
palette256* palette256_new(notcurses* nc){
ncpalette* ncpalette_new(notcurses* nc){
palette256* p = malloc(sizeof(*p));
if(p){
memcpy(p, &nc->palette, sizeof(*p));
@ -2087,7 +2087,11 @@ palette256* palette256_new(notcurses* nc){
return p;
}
int palette256_use(notcurses* nc, const palette256* p){
ncpalette* palette256_new(notcurses* nc){
return ncpalette_new(nc);
}
int ncpalette_use(notcurses* nc, const ncpalette* p){
int ret = -1;
if(!notcurses_canchangecolor(nc)){
return -1;
@ -2102,10 +2106,18 @@ int palette256_use(notcurses* nc, const palette256* p){
return ret;
}
void palette256_free(palette256* p){
int palette256_use(notcurses* nc, const palette256* p){
return ncpalette_use(nc, p);
}
void ncpalette_free(ncpalette* p){
free(p);
}
void palette256_free(ncpalette* p){
ncpalette_free(p);
}
bool ncplane_translate_abs(const ncplane* n, int* restrict y, int* restrict x){
ncplane_translate(ncplane_stdplane_const(n), n, y, x);
if(y){

View File

@ -9,33 +9,33 @@ TEST_CASE("Palette256") {
REQUIRE(n_);
SUBCASE("AllocAndFree") {
palette256* p = palette256_new(nc_);
ncpalette* p = ncpalette_new(nc_);
REQUIRE(nullptr != p);
palette256_free(p);
ncpalette_free(p);
}
SUBCASE("SetIndexZero") {
palette256* p = palette256_new(nc_);
ncpalette* p = ncpalette_new(nc_);
REQUIRE(nullptr != p);
palette256_set_rgb8(p, 0, 0x80, 0x90, 0xa0);
ncpalette_set_rgb8(p, 0, 0x80, 0x90, 0xa0);
unsigned r, g, b;
palette256_get_rgb8(p, 0, &r, &g, &b);
ncpalette_get_rgb8(p, 0, &r, &g, &b);
CHECK(r == 0x80);
CHECK(g == 0x90);
CHECK(b == 0xa0);
palette256_free(p);
ncpalette_free(p);
}
SUBCASE("SetIndex255") {
palette256* p = palette256_new(nc_);
ncpalette* p = ncpalette_new(nc_);
REQUIRE(nullptr != p);
palette256_set_rgb8(p, 255, 0xa0, 0x70, 0x50);
ncpalette_set_rgb8(p, 255, 0xa0, 0x70, 0x50);
unsigned r, g, b;
palette256_get_rgb8(p, 255, &r, &g, &b);
ncpalette_get_rgb8(p, 255, &r, &g, &b);
CHECK(r == 0xa0);
CHECK(g == 0x70);
CHECK(b == 0x50);
palette256_free(p);
ncpalette_free(p);
}
// when we set a palette index, it ought change us from using default