rust-osdev / ansi_rgb Goto Github PK
View Code? Open in Web Editor NEWColorful terminal text using ANSI escape sequences
Home Page: https://docs.rs/ansi_rgb/
License: MIT License
Colorful terminal text using ANSI escape sequences
Home Page: https://docs.rs/ansi_rgb/
License: MIT License
Take cues from https://github.com/rust-osdev/x86_64
0.2.0 is ready to be published
Can convert from HSV with this:
use ansi_rgb::*;
use num_traits::{ Float, Num };
fn pos_mod<T: Num + Copy>(x: T, modulo: T) -> T {
(x % modulo + modulo) % modulo
}
fn from_hsv(h: f32, s: f32, v: f32) -> Rgb {
// https://www.rapidtables.com/convert/color/hsv-to-rgb.html
let h = pos_mod(h, 360.0);
let s = pos_mod(s, 1.0 + f32::epsilon());
let v = pos_mod(v, 1.0 + f32::epsilon());
let c = v * s;
let x = c * (1.0 - ((h / 60.0) % 2.0 - 1.0).abs());
let m = v - c;
let (r, g, b) = match h {
h if h < 60.0 => (c, x, 0.0),
h if h < 120.0 => (x, c, 0.0),
h if h < 180.0 => (0.0, c, x),
h if h < 240.0 => (0.0, x, c),
h if h < 300.0 => (x, 0.0, c),
_ => (c, 0.0, x)
};
Rgb::new(
((r + m) * 255.0) as u8,
((g + m) * 255.0) as u8,
((b + m) * 255.0) as u8
)
}
It supports no_std
. Others are using it. Will make it easier for others to interface with this crate, like if they want to use a color conversion function (#6)
use ansi_rgb::{ Background, Foreground, red, green };
fn main() {
let foo = "Hello, world!".fg(green()).bg(red());
println!("{}", foo);
}
Output:
error[E0716]: temporary value dropped while borrowed
--> src\main.rs:4:15
|
4 | let foo = "Hello, world!".fg(green()).bg(red());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
5 | println!("{}", foo);
| --- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
In a Windows console you just get garbage like this:
�[48;2;159;114;0m �[0m
Probably need to investigate https://docs.microsoft.com/en-us/windows/console/console-modes
Perhaps this functionality needs to go into another crate (or perhaps someone has already implemented this in another crate)
This is a library. Give main.rs
the axe
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.