Code Monkey home page Code Monkey logo

ansi_rgb's Introduction

ansi_rgb's People

Contributors

hanmertens avatar matthew-a-thomas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ansi_rgb's Issues

Fix formatting of pointers

use ansi_rgb::{ Background, Foreground, red, green };

fn main() {
    println!("{:p}", "Hello, world!".fg(green()).bg(red()));
}

Output missing foreground color

"temporary value dropped while borrowed"

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

Color conversion functions

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
    )
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.