Use ansi_colours package for better true-colour approximation

This commit is contained in:
Michal Nazarewicz 2018-09-21 15:56:09 +01:00 committed by David Peter
parent 6aa626f1c4
commit 79b960e17e
3 changed files with 14 additions and 44 deletions

10
Cargo.lock generated
View File

@ -6,6 +6,14 @@ dependencies = [
"memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ansi_colours"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
@ -37,6 +45,7 @@ dependencies = [
name = "bat"
version = "0.7.0"
dependencies = [
"ansi_colours 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -794,6 +803,7 @@ dependencies = [
[metadata]
"checksum aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "68f56c7353e5a9547cbd76ed90f7bb5ffc3ba09d4ea9bd1d8c06c8b1142eeb5a"
"checksum ansi_colours 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1d0f302a81afc6a7f4350c04f0ba7cfab529cc009bca3324b3fb5764e6add8b6"
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
"checksum base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c4a342b450b268e1be8036311e2c613d7f8a7ed31214dff1cc3b60852a3168d"

View File

@ -16,6 +16,7 @@ exclude = [
[dependencies]
atty = "0.2.2"
ansi_term = "0.11"
ansi_colours = "^1.0"
console = "0.6"
directories = "1.0"
lazy_static = "1.0"

View File

@ -1,32 +1,15 @@
extern crate ansi_colours;
use ansi_term::Colour::{Fixed, RGB};
use ansi_term::{self, Style};
use syntect::highlighting::{self, FontStyle};
/// Approximate a 24 bit color value by a 8 bit ANSI code
fn rgb2ansi(r: u8, g: u8, b: u8) -> u8 {
const BLACK: u8 = 16;
const WHITE: u8 = 231;
if r == g && g == b {
if r < 8 {
BLACK
} else if r > 248 {
WHITE
} else {
((r - 8) as u16 * 24 / 247) as u8 + 232
}
} else {
36 * (r / 51) + 6 * (g / 51) + (b / 51) + 16
}
}
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term::Colour {
if true_color {
RGB(color.r, color.g, color.b)
} else {
let ansi_code = rgb2ansi(color.r, color.g, color.b);
Fixed(ansi_code)
Fixed(ansi_colours::ansi256_from_rgb((color.r, color.g, color.b)))
}
}
@ -54,27 +37,3 @@ pub fn as_terminal_escaped(
style.paint(text).to_string()
}
#[test]
fn test_rgb2ansi_black_white() {
assert_eq!(16, rgb2ansi(0x00, 0x00, 0x00));
assert_eq!(231, rgb2ansi(0xff, 0xff, 0xff));
}
#[test]
fn test_rgb2ansi_gray() {
assert_eq!(241, rgb2ansi(0x6c, 0x6c, 0x6c));
assert_eq!(233, rgb2ansi(0x1c, 0x1c, 0x1c));
}
#[test]
fn test_rgb2ansi_color() {
assert_eq!(96, rgb2ansi(0x87, 0x5f, 0x87));
assert_eq!(141, rgb2ansi(0xaf, 0x87, 0xff));
assert_eq!(193, rgb2ansi(0xd7, 0xff, 0xaf));
}
#[test]
fn test_rgb2ansi_approx() {
assert_eq!(231, rgb2ansi(0xfe, 0xfe, 0xfe));
}