on master help sign in
Use ansi_colours package for better true-colour approximation
commit79b960e17e75339d2093a78bc7f8af89a05a2018 authorMichal Nazarewicz <mina86@mina86.com> dateFri 21 Sep 2018, 15:56 (8 years ago) parent6aa626f
3 files changed+14−44
Cargo.lock+10 Cargo.toml+1 src/terminal.rs+3−44
Cargo.lock+10
@@ -6,6 +6,14 @@ dependencies = [
66 "memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
77]
88
9+[[package]]
10+name = "ansi_colours"
11+version = "1.0.1"
12+source = "registry+https://github.com/rust-lang/crates.io-index"
13+dependencies = [
14+ "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
15+]
16+
917[[package]]
1018name = "ansi_term"
1119version = "0.11.0"
@@ -37,6 +45,7 @@ dependencies = [
3745name = "bat"
3846version = "0.7.0"
3947dependencies = [
48+ "ansi_colours 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
4049 "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
4150 "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
4251 "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -794,6 +803,7 @@ dependencies = [
794803
795804[metadata]
796805"checksum aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "68f56c7353e5a9547cbd76ed90f7bb5ffc3ba09d4ea9bd1d8c06c8b1142eeb5a"
806+"checksum ansi_colours 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1d0f302a81afc6a7f4350c04f0ba7cfab529cc009bca3324b3fb5764e6add8b6"
797807"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
798808"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
799809"checksum base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c4a342b450b268e1be8036311e2c613d7f8a7ed31214dff1cc3b60852a3168d"
Cargo.toml+1
@@ -16,6 +16,7 @@ exclude = [
1616[dependencies]
1717atty = "0.2.2"
1818ansi_term = "0.11"
19+ansi_colours = "^1.0"
1920console = "0.6"
2021directories = "1.0"
2122lazy_static = "1.0"
src/terminal.rs+3−44
@@ -1,32 +1,15 @@
1+extern crate ansi_colours;
2+
13use ansi_term::Colour::{Fixed, RGB};
24use ansi_term::{self, Style};
35
46use syntect::highlighting::{self, FontStyle};
57
6−/// Approximate a 24 bit color value by a 8 bit ANSI code
7−fn rgb2ansi(r: u8, g: u8, b: u8) -> u8 {
8− const BLACK: u8 = 16;
9− const WHITE: u8 = 231;
10−
11− if r == g && g == b {
12− if r < 8 {
13− BLACK
14− } else if r > 248 {
15− WHITE
16− } else {
17− ((r - 8) as u16 * 24 / 247) as u8 + 232
18− }
19− } else {
20− 36 * (r / 51) + 6 * (g / 51) + (b / 51) + 16
21− }
22−}
23−
248pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term::Colour {
259 if true_color {
2610 RGB(color.r, color.g, color.b)
2711 } else {
28− let ansi_code = rgb2ansi(color.r, color.g, color.b);
29− Fixed(ansi_code)
12+ Fixed(ansi_colours::ansi256_from_rgb((color.r, color.g, color.b)))
3013 }
3114}
3215
@@ -54,27 +37,3 @@ pub fn as_terminal_escaped(
5437
5538 style.paint(text).to_string()
5639}
57−
58−#[test]
59−fn test_rgb2ansi_black_white() {
60− assert_eq!(16, rgb2ansi(0x00, 0x00, 0x00));
61− assert_eq!(231, rgb2ansi(0xff, 0xff, 0xff));
62−}
63−
64−#[test]
65−fn test_rgb2ansi_gray() {
66− assert_eq!(241, rgb2ansi(0x6c, 0x6c, 0x6c));
67− assert_eq!(233, rgb2ansi(0x1c, 0x1c, 0x1c));
68−}
69−
70−#[test]
71−fn test_rgb2ansi_color() {
72− assert_eq!(96, rgb2ansi(0x87, 0x5f, 0x87));
73− assert_eq!(141, rgb2ansi(0xaf, 0x87, 0xff));
74− assert_eq!(193, rgb2ansi(0xd7, 0xff, 0xaf));
75−}
76−
77−#[test]
78−fn test_rgb2ansi_approx() {
79− assert_eq!(231, rgb2ansi(0xfe, 0xfe, 0xfe));
80−}