Generate colour themes for Emacs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.rs 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2018 Inderjit Gill
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, either version 3 of the License, or
  5. // (at your option) any later version.
  6. // This program is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. // GNU General Public License for more details.
  10. // You should have received a copy of the GNU General Public License
  11. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  12. #![allow(dead_code)]
  13. #![cfg_attr(
  14. feature = "cargo-clippy",
  15. allow(many_single_char_names, too_many_arguments)
  16. )]
  17. extern crate cfg_if;
  18. extern crate wasm_bindgen;
  19. mod colour;
  20. mod error;
  21. mod utils;
  22. use crate::colour::{Colour, Format};
  23. use cfg_if::cfg_if;
  24. use wasm_bindgen::prelude::*;
  25. cfg_if! {
  26. // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
  27. // allocator.
  28. if #[cfg(feature = "wee_alloc")] {
  29. extern crate wee_alloc;
  30. #[global_allocator]
  31. static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
  32. }
  33. }
  34. #[wasm_bindgen]
  35. extern "C" {
  36. #[wasm_bindgen(js_namespace = console)]
  37. fn log(s: &str);
  38. }
  39. #[wasm_bindgen]
  40. pub struct Transport3C {
  41. c0: f64,
  42. c1: f64,
  43. c2: f64,
  44. }
  45. #[wasm_bindgen]
  46. impl Transport3C {
  47. pub fn new(c0: f64, c1: f64, c2: f64) -> Transport3C {
  48. Transport3C {
  49. c0: c0,
  50. c1: c1,
  51. c2: c2,
  52. }
  53. }
  54. pub fn blank() -> Transport3C {
  55. Transport3C {
  56. c0: 0.0,
  57. c1: 0.0,
  58. c2: 0.0,
  59. }
  60. }
  61. pub fn get_0(&self) -> f64 {
  62. self.c0
  63. }
  64. pub fn get_1(&self) -> f64 {
  65. self.c1
  66. }
  67. pub fn get_2(&self) -> f64 {
  68. self.c2
  69. }
  70. }
  71. #[wasm_bindgen]
  72. pub fn hsl_from_rgb(r: f64, g: f64, b: f64) -> Transport3C {
  73. let hsluv = Colour::RGB(r, g, b, 1.0).clone_as(Format::HSLuv).unwrap();
  74. match hsluv {
  75. Colour::HSLuv(h, s, l, _) => Transport3C::new(h, s, l),
  76. _ => Transport3C::blank(),
  77. }
  78. }
  79. #[wasm_bindgen]
  80. pub fn rgb_from_hsl(h: f64, s: f64, l: f64) -> Transport3C {
  81. let rgb = Colour::HSLuv(h, s, l, 1.0).clone_as(Format::RGB).unwrap();
  82. match rgb {
  83. Colour::RGB(r, g, b, _) => Transport3C::new(r, g, b),
  84. _ => Transport3C::blank(),
  85. }
  86. }