Skip to content

Commit

Permalink
Merge commit '82b3893c2a8df9c1540aa78c0303e0ba85aad84b'
Browse files Browse the repository at this point in the history
  • Loading branch information
jgardona committed Jan 9, 2024
2 parents 424a255 + 82b3893 commit 9cc5e27
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
71 changes: 50 additions & 21 deletions examples/truecolor.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,67 @@
use std::io::{self, Read, Seek, Write};

use ansistream::{FCDARKGRAY, BCBLACK};

fn flush_stdout<W: Read + Seek>(reader: &mut W) -> io::Result<()> {
let mut stdout = io::stdout().lock();
reader.seek(io::SeekFrom::Start(0))?;
io::copy(reader, &mut stdout)?;
Ok(())
}

fn hex2rgb(hex: u32) -> (u16, u16, u16) {
let r = (hex >> 16) as u16;
let g = ((hex >> 8) & 0xff) as u16;
let b = (hex & 0xff) as u16;

(r, g, b)
}

fn main() -> io::Result<()> {
let mut astream = ansistream::AnsiEscapeStream::new(2000);

let palettes: [u32; 20] = [
0xf1c15d,
0x85a746,
0x599a70,
0xe56a4b,
0xeda052,
0xb3d9e2,
0xb3b4ac,
0xfefacb,
0x78495d,
0x93667a,
0x36473d,
0x778c63,
0xe8df7a,
0xe6a91b,
0xea9804,
0x242623,
0x7d603e,
0xdfd1a4,
0x5e7e4f,
0xc74223
];

astream.write_string("Printing a common brazilian color palette:\n\n")?;

astream.write_text_fcrgb(50, 50, 50, "")?;
astream.write_text_bcrgb(139, 60, 60, " #8b3c3c ")?;
writeln!(&mut *astream)?;
astream.write_text_bcrgb(189, 90, 92, " #bd5a5c ")?;
writeln!(&mut *astream)?;
astream.write_text_bcrgb(64, 80, 103, " #405067 ")?;
writeln!(&mut *astream)?;
astream.write_text_bcrgb(98, 112, 121, " #627079 ")?;
writeln!(&mut *astream)?;
astream.write_text_bcrgb(156, 118, 53, " #9c7635 ")?;
writeln!(&mut *astream)?;
astream.write_text_bcrgb(211, 189, 152, " #d3bd98 ")?;
writeln!(&mut *astream)?;
astream.write_text_bcrgb(130, 101, 103, " #826567 ")?;
writeln!(&mut *astream)?;
astream.write_text_bcrgb(169, 139, 139, " #a98b8b ")?;
writeln!(&mut *astream)?;
astream.write_text_bcrgb(255, 180, 1, " #ffb401 ")?;
writeln!(&mut *astream)?;
astream.write_text_bcrgb(255, 217, 115, " #ffd973 ")?;
writeln!(&mut *astream)?;
for (idx, &c) in palettes.iter().enumerate() {
if idx % 5 == 0 {
astream.reset_attribute(BCBLACK)?;
astream.write_string("\t")?;
}

if idx % 10 == 0 {
astream.reset_attribute(BCBLACK)?;
writeln!(&mut *astream)?;
}
astream.write_text_fc(FCDARKGRAY, "")?;
let rgb = hex2rgb(c);
astream.write_text_bcrgb(rgb.0, rgb.1, rgb.2, "")?;
write!(&mut *astream, " {c:#06x}")?;
}

astream.reset_all_attributes()?;

flush_stdout(&mut *astream)?;
Ok(())
Expand Down
Binary file modified images/truecolor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9cc5e27

Please sign in to comment.