@@ -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−}