Browse Source

formatting

master
Inderjit Gill 5 months ago
parent
commit
2c055346bb
3 changed files with 24 additions and 31 deletions
  1. +14
    -19
      core/src/colour.rs
  2. +1
    -3
      core/src/native.rs
  3. +9
    -9
      utils/palette-maker/src/main.rs

+ 14
- 19
core/src/colour.rs View File

@@ -238,15 +238,20 @@ impl Colour {
// hex in the form: "ff00ff"
pub fn from_rgb_hex(hex: &str) -> Result<Self> {
if hex.len() != 6 {
error!("Colour::from_rgb_hex expects input as 6 hex digits, actual: {}", hex);
return Err(Error::Colour)
error!(
"Colour::from_rgb_hex expects input as 6 hex digits, actual: {}",
hex
);
return Err(Error::Colour);
}

Ok(Colour::new(ColourFormat::Rgb,
normalised_colour_from_hex_string(&hex[0..2])?,
normalised_colour_from_hex_string(&hex[2..4])?,
normalised_colour_from_hex_string(&hex[4..])?,
1.0))
Ok(Colour::new(
ColourFormat::Rgb,
normalised_colour_from_hex_string(&hex[0..2])?,
normalised_colour_from_hex_string(&hex[2..4])?,
normalised_colour_from_hex_string(&hex[4..])?,
1.0,
))
}

pub fn convert(&self, format: ColourFormat) -> Result<Colour> {
@@ -1128,12 +1133,7 @@ mod tests {
)
}

fn assert_colour(col: Colour,
format: ColourFormat,
e0: f32,
e1: f32,
e2: f32,
e3: f32) {
fn assert_colour(col: Colour, format: ColourFormat, e0: f32, e1: f32, e2: f32, e3: f32) {
is_format(col.format, format);
assert_eq!(col.e0, e0);
assert_eq!(col.e1, e1);
@@ -1141,12 +1141,7 @@ mod tests {
assert_eq!(col.e3, e3);
}

fn assert_hex_colour(hex: &str,
format: ColourFormat,
e0: f32,
e1: f32,
e2: f32,
e3: f32) {
fn assert_hex_colour(hex: &str, format: ColourFormat, e0: f32, e1: f32, e2: f32, e3: f32) {
if let Ok(col) = Colour::from_rgb_hex(hex) {
assert_colour(col, format, e0, e1, e2, e3);
} else {

+ 1
- 3
core/src/native.rs View File

@@ -1989,9 +1989,7 @@ fn col_value_execute(vm: &mut Vm) -> Result<Option<Var>> {
fn col_palette_parameter_info() -> Result<(Vec<(Keyword, Var)>, i32)> {
Ok((
// input arguments
vec![
(Keyword::Index, Var::Float(0.0)),
],
vec![(Keyword::Index, Var::Float(0.0))],
// stack offset
1,
))

+ 9
- 9
utils/palette-maker/src/main.rs View File

@@ -13,6 +13,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

/// read in a json file from https://github.com/Jam3/nice-color-palettes.git
/// and output a rust file which lists the palettes in hsluv colour space

use clap::{App, Arg};
use core::colour::{Colour, ColourFormat};
use serde_json;
@@ -49,25 +52,22 @@ fn main() -> Result<()> {

fn run(json_filename: &str, rs_filename: &str) -> Result<()> {
let contents = std::fs::read_to_string(json_filename)?;

let rgb_palettes: Vec<Vec<String>> = serde_json::from_str(&contents)?;
let hsluv_palettes: Vec<Vec<Colour>> = convert_palettes(rgb_palettes)?;
// println!("{:#?}", hsluv_palettes);

let source_code = build_source(&hsluv_palettes)?;
let json_palettes: Vec<Vec<String>> = serde_json::from_str(&contents)?;
let converted_palettes: Vec<Vec<Colour>> = convert_palettes(json_palettes)?;
let source_code = build_source(&converted_palettes)?;

std::fs::write(rs_filename, source_code)?;

Ok(())
}

fn build_source(hsluv_palettes: &Vec<Vec<Colour>>) -> Result<String> {
let source_size = estimate_output_size(hsluv_palettes.len());
fn build_source(palettes: &Vec<Vec<Colour>>) -> Result<String> {
let source_size = estimate_output_size(palettes.len());
let mut source = String::with_capacity(source_size);

source.push_str(get_header());

for palette in hsluv_palettes {
for palette in palettes {
source.push_str(" &[\n");
for colour in palette {
source.push_str(" Colour {\n");

Loading…
Cancel
Save