Skip to content

Commit

Permalink
Minor comment wording improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mina86 committed Apr 25, 2024
1 parent 3f56b28 commit b9feefc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
25 changes: 11 additions & 14 deletions src/ansi256.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ uint8_t ansi256_from_rgb(uint32_t rgb) {
}


/* The next three functions approximate a pure colour by a colour in the 6×6×6
colour cube. E.g. cube_index_red(r) approximates an rgb(r, 0, 0) colour.
This was motivated by ΔE*₀₀ being most variable in dark colours so I felt
it’s more important to better approximate dark colours than white colours.
/* The next three functions approximate a pure colour by an entry in the 6×6×6
cube. E.g. cube_index_red(r) approximates an rgb(r, 0, 0) colour. This was
motivated by ΔE*₀₀ being most variable in dark colours so I felt it’s more
important to better approximate dark colours than light colours.
The return values of the functions is kinda weird but it makes
ansi256_from_rgb a bit shorter, as in having to do a bit fewer things. */
Expand All @@ -189,35 +189,32 @@ uint8_t ansi256_from_rgb(uint32_t rgb) {
else if (v < e) return IDX(4, 215); \
else return IDX(5, 255);

#define IDX(i, v) ((((uint32_t)i * 36 + 16) << 24) | ((uint32_t)v << 16))

static uint32_t cube_index_red(uint8_t v) {
# define IDX(i, v) ((((uint32_t)i * 36 + 16) << 24) | ((uint32_t)v << 16))
CUBE_THRESHOLDS(38, 115, 155, 196, 235);
# undef IDX
}

#undef IDX
#define IDX(i, v) ((((uint32_t)i * 6) << 24) | ((uint32_t)v << 8))

static uint32_t cube_index_green(uint8_t v) {
# define IDX(i, v) ((((uint32_t)i * 6) << 24) | ((uint32_t)v << 8))
CUBE_THRESHOLDS(36, 116, 154, 195, 235);
# undef IDX
}

#undef IDX
#define IDX(i, v) (((uint32_t)i << 24) | (uint32_t)v)

static uint32_t cube_index_blue(uint8_t v) {
# define IDX(i, v) (((uint32_t)i << 24) | (uint32_t)v)
CUBE_THRESHOLDS(35, 115, 155, 195, 235);
# undef IDX
}

#undef IDX
#undef CUBE_THRESHOLDS


/* Returns luminance of given sRGB colour. The calculation favours speed over
precision and so doesn’t correctly account for sRGB’s gamma correction. */
static uint8_t luminance(uint32_t rgb) {
/* The following weighted average is as fast as naive arithmetic mean
and at the same time noticeably more prices. The coefficients are
and at the same time noticeably more precise. The coefficients are
the second row of the RGB->XYZ conversion matrix (i.e. values for
calculating Y from linear RGB) which I’ve calculated so that
denominator is 2^24 to simplify division. */
Expand Down
10 changes: 5 additions & 5 deletions src/ansi256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ fn cube_thresholds(v: u8, a: u8, b: u8, c: u8, d: u8, e: u8) -> (u8, u32) {
else { (5, 255) }
}

// The next three functions approximate a pure colour by a colour in the 6×6×6
// colour cube. E.g. cube_index_red(r) approximates an rgb(r, 0, 0) colour.
// This was motivated by ΔE*₀₀ being most variable in dark colours so I felt
// it’s more important to better approximate dark colours than white colours.
// The next three functions approximate a pure colour by an entry in the 6×6×6
// cube. E.g. cube_index_red(r) approximates an rgb(r, 0, 0) colour. This was
// motivated by ΔE*₀₀ being most variable in dark colours so I felt it’s more
// important to better approximate dark colours than light colours.

fn cube_index_red(v: u8) -> (u8, u32) {
let (i, v) = cube_thresholds(v, 38, 115, 155, 196, 235);
Expand All @@ -201,7 +201,7 @@ fn cube_index_blue(v: u8) -> (u8, u32) {
/// precision and so doesn’t correctly account for sRGB’s gamma correction.
fn luminance(r: u8, g: u8, b: u8) -> u8 {
// The following weighted average is as fast as naive arithmetic mean and at
// the same time noticeably more prices. The coefficients are the second
// the same time noticeably more precise. The coefficients are the second
// row of the RGB->XYZ conversion matrix (i.e. values for calculating Y from
// linear RGB) which I’ve calculated so that denominator is 2^24 to simplify
// division.
Expand Down

0 comments on commit b9feefc

Please sign in to comment.