plate-tool/src/components/plates/util.rs

77 lines
2.3 KiB
Rust
Raw Normal View History

2023-06-06 01:33:23 +00:00
// Sources:
// https://iquilezles.org/articles/palettes/
// http://dev.thi.ng/gradients/
Merge from import_from_csv feature branch Of course, there were other features that got tacked on... Squashed commit of the following: commit 3ee3bd2dab3ed362bf5be9acfea8db1f16f6fc9c Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 19:12:16 2023 -0500 Superior clipboard manipulation Won't work on non-https connections, but actually works... commit 08f647cd01566e9000d71f677df0d96411ed4cd4 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:50:01 2023 -0500 Utility for copying plates as image commit 3456be2e9a07f63bf5d62ca685fe692303875888 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 17:46:38 2023 -0500 Change wording in options menu Father suggests that this wording is more clear to the end user. I agree! commit 4c79cc0b4dd5402a798ce8d7ac69599c03cf49d9 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:20:00 2023 -0500 Set default plate format to 96 well commit 056688c4ec690a384f57c194314cb924253770f5 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:12:00 2023 -0500 Implement in_transfer hashes toggle in plates commit 4937d4ad283b90629a80a213595a873d1a3a2615 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:11:00 2023 -0500 Preferences menu and toggle for in_transfer hashes commit 0101846b52393d1a050c2c7672274b4589c5c993 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:10:00 2023 -0500 Add preferences struct to main state commit ec37887c2f7e834ed7456625a9ec4c4cc7fea08f Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:05:00 2023 -0500 Squashed commit of the following: commit 5e1137c46050d6d1d10fb1cbf842d5efd455f926 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:03:00 2023 -0500 Fix: indexing error w.r.t. logarithm argument commit 535b14a5867b15dea0e193c94829a0b99a29605c Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:02:00 2023 -0500 Space colors evenly, consistently, etc Colors should now: - Not change if new transfers are added - Be evenly spaced throughout the palette - Be persistent across refreshes commit 6e08f479551315e072ec40853290d699220c9ba8 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:01:00 2023 -0500 Add palette function for ordered ids Given an id and a list of sorted ids, yields a color commit 88e838e102602f4d89ab65d8245a820158374959 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:00:00 2023 -0500 Switch to v7 UUIDs from v4 v7 UUIDs are timestamp based and thus we can establish a useful total ordering over them; will base colors on this commit 85d4b30d4795636add23a6a2c47b2068e24dbb74 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 21:18:10 2023 -0400 Update README.md Updated info about import/export, including the new Import Transfer from CSV feature. commit 11a561c1d447e1376e58f970f74d0778e8ed1722 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 20:32:51 2023 -0400 Add text to button commit 562dc2adf60b8569da579b1bb637cea344a7a596 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 20:32:40 2023 -0400 Change to make colors more evenly distributed commit 6b09aad289b83576bdf67882ee3f30b7f1d23619 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 19:27:02 2023 -0400 Implementation 1 commit a9e5f05fd9c0ebcbbb1c3cc86af8f30c92f373c5 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 17:18:45 2023 -0400 Hide parts of transfer menu when Custom transfer selected commit db345bfbb585c826c6f97424c5ee61337c4ffbfd Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 17:18:08 2023 -0400 delete weird whitespace from Cargo.toml commit edcc3528aab3b17aedaf7b3424ece82ad09d591a Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 16:41:58 2023 -0400 First implementation of custom region type commit 9a3a10c8b43e51471f74f79dfbef66b3f69ea0f5 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 16:21:30 2023 -0400 Transfer region no longer copy
2023-12-30 01:39:00 +00:00
use rand::prelude::*;
use rand::rngs::SmallRng;
use lazy_static::lazy_static;
2023-06-06 01:33:23 +00:00
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct ColorPalette {
a: [f64; 3],
b: [f64; 3],
c: [f64; 3],
d: [f64; 3],
}
impl ColorPalette {
2023-06-07 21:17:40 +00:00
pub fn _new(a: [f64; 3], b: [f64; 3], c: [f64; 3], d: [f64; 3]) -> Self {
2023-06-06 01:33:23 +00:00
ColorPalette { a, b, c, d }
}
pub fn get(&self, t: f64) -> [f64; 3] {
[
2023-06-08 15:57:03 +00:00
(self.a[0] + self.b[0] * f64::cos(std::f64::consts::TAU * (self.c[0] * t + self.d[0])))
* 255.0,
(self.a[1] + self.b[1] * f64::cos(std::f64::consts::TAU * (self.c[1] * t + self.d[1])))
* 255.0,
(self.a[2] + self.b[2] * f64::cos(std::f64::consts::TAU * (self.c[2] * t + self.d[2])))
* 255.0,
2023-06-06 01:33:23 +00:00
]
}
2023-06-16 02:23:12 +00:00
pub fn _get_u8(&self, t: u8) -> [f64; 3] {
assert!(t > 0, "t must be greater than zero!");
2023-06-16 02:23:12 +00:00
self.get((2f64.powi(-(t.ilog2() as i32))) * (t as f64 + 0.5f64) - 1.0f64)
2023-06-06 01:33:23 +00:00
}
Merge from import_from_csv feature branch Of course, there were other features that got tacked on... Squashed commit of the following: commit 3ee3bd2dab3ed362bf5be9acfea8db1f16f6fc9c Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 19:12:16 2023 -0500 Superior clipboard manipulation Won't work on non-https connections, but actually works... commit 08f647cd01566e9000d71f677df0d96411ed4cd4 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:50:01 2023 -0500 Utility for copying plates as image commit 3456be2e9a07f63bf5d62ca685fe692303875888 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 17:46:38 2023 -0500 Change wording in options menu Father suggests that this wording is more clear to the end user. I agree! commit 4c79cc0b4dd5402a798ce8d7ac69599c03cf49d9 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:20:00 2023 -0500 Set default plate format to 96 well commit 056688c4ec690a384f57c194314cb924253770f5 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:12:00 2023 -0500 Implement in_transfer hashes toggle in plates commit 4937d4ad283b90629a80a213595a873d1a3a2615 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:11:00 2023 -0500 Preferences menu and toggle for in_transfer hashes commit 0101846b52393d1a050c2c7672274b4589c5c993 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:10:00 2023 -0500 Add preferences struct to main state commit ec37887c2f7e834ed7456625a9ec4c4cc7fea08f Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:05:00 2023 -0500 Squashed commit of the following: commit 5e1137c46050d6d1d10fb1cbf842d5efd455f926 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:03:00 2023 -0500 Fix: indexing error w.r.t. logarithm argument commit 535b14a5867b15dea0e193c94829a0b99a29605c Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:02:00 2023 -0500 Space colors evenly, consistently, etc Colors should now: - Not change if new transfers are added - Be evenly spaced throughout the palette - Be persistent across refreshes commit 6e08f479551315e072ec40853290d699220c9ba8 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:01:00 2023 -0500 Add palette function for ordered ids Given an id and a list of sorted ids, yields a color commit 88e838e102602f4d89ab65d8245a820158374959 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:00:00 2023 -0500 Switch to v7 UUIDs from v4 v7 UUIDs are timestamp based and thus we can establish a useful total ordering over them; will base colors on this commit 85d4b30d4795636add23a6a2c47b2068e24dbb74 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 21:18:10 2023 -0400 Update README.md Updated info about import/export, including the new Import Transfer from CSV feature. commit 11a561c1d447e1376e58f970f74d0778e8ed1722 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 20:32:51 2023 -0400 Add text to button commit 562dc2adf60b8569da579b1bb637cea344a7a596 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 20:32:40 2023 -0400 Change to make colors more evenly distributed commit 6b09aad289b83576bdf67882ee3f30b7f1d23619 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 19:27:02 2023 -0400 Implementation 1 commit a9e5f05fd9c0ebcbbb1c3cc86af8f30c92f373c5 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 17:18:45 2023 -0400 Hide parts of transfer menu when Custom transfer selected commit db345bfbb585c826c6f97424c5ee61337c4ffbfd Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 17:18:08 2023 -0400 delete weird whitespace from Cargo.toml commit edcc3528aab3b17aedaf7b3424ece82ad09d591a Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 16:41:58 2023 -0400 First implementation of custom region type commit 9a3a10c8b43e51471f74f79dfbef66b3f69ea0f5 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 16:21:30 2023 -0400 Transfer region no longer copy
2023-12-30 01:39:00 +00:00
// pub fn get_uuid(&self, t: uuid::Uuid) -> [f64; 3] {
// // self.get(t.as_u128() as f64 / (u128::MAX) as f64)
// let mut r = SmallRng::seed_from_u64(t.as_u128() as u64);
// self.get(r.gen_range(0.0..1.0f64))
// }
pub fn get_ordered(&self, t: uuid::Uuid, ordered_uuids: &Vec<uuid::Uuid>)
-> [f64; 3] {
let index = ordered_uuids.iter().position(|&x| x == t).expect("uuid must be in list of uuids") + 1;
return self.get(Self::space_evenly(index))
}
fn space_evenly(x: usize) -> f64 {
let e: usize = (x.ilog2() + 1) as usize;
let d: usize = (2usize.pow(e as u32)) as usize;
let n: usize = (2*x + 1) % d;
return (n as f64) / (d as f64);
}
2023-06-06 01:33:23 +00:00
}
#[non_exhaustive]
pub struct Palettes;
#[allow(dead_code)]
impl Palettes {
pub const RAINBOW: ColorPalette = ColorPalette {
a: [0.500, 0.500, 0.500],
2023-06-13 23:39:18 +00:00
b: [0.700, 0.700, 0.700],
c: [0.800, 0.800, 0.800],
d: [0.000, 0.333, 0.667],
2023-06-06 01:33:23 +00:00
};
pub const YELLOW_PINK: ColorPalette = ColorPalette {
a: [0.500, 0.500, 0.320],
b: [0.500, 0.500, 0.500],
c: [0.100, 0.500, 0.360],
d: [0.000, 0.000, 0.650],
2023-06-06 01:33:23 +00:00
};
Merge from import_from_csv feature branch Of course, there were other features that got tacked on... Squashed commit of the following: commit 3ee3bd2dab3ed362bf5be9acfea8db1f16f6fc9c Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 19:12:16 2023 -0500 Superior clipboard manipulation Won't work on non-https connections, but actually works... commit 08f647cd01566e9000d71f677df0d96411ed4cd4 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:50:01 2023 -0500 Utility for copying plates as image commit 3456be2e9a07f63bf5d62ca685fe692303875888 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 17:46:38 2023 -0500 Change wording in options menu Father suggests that this wording is more clear to the end user. I agree! commit 4c79cc0b4dd5402a798ce8d7ac69599c03cf49d9 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:20:00 2023 -0500 Set default plate format to 96 well commit 056688c4ec690a384f57c194314cb924253770f5 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:12:00 2023 -0500 Implement in_transfer hashes toggle in plates commit 4937d4ad283b90629a80a213595a873d1a3a2615 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:11:00 2023 -0500 Preferences menu and toggle for in_transfer hashes commit 0101846b52393d1a050c2c7672274b4589c5c993 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:10:00 2023 -0500 Add preferences struct to main state commit ec37887c2f7e834ed7456625a9ec4c4cc7fea08f Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:05:00 2023 -0500 Squashed commit of the following: commit 5e1137c46050d6d1d10fb1cbf842d5efd455f926 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:03:00 2023 -0500 Fix: indexing error w.r.t. logarithm argument commit 535b14a5867b15dea0e193c94829a0b99a29605c Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:02:00 2023 -0500 Space colors evenly, consistently, etc Colors should now: - Not change if new transfers are added - Be evenly spaced throughout the palette - Be persistent across refreshes commit 6e08f479551315e072ec40853290d699220c9ba8 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:01:00 2023 -0500 Add palette function for ordered ids Given an id and a list of sorted ids, yields a color commit 88e838e102602f4d89ab65d8245a820158374959 Author: Emilia <contact@emiliaallison.com> Date: Fri Dec 29 18:00:00 2023 -0500 Switch to v7 UUIDs from v4 v7 UUIDs are timestamp based and thus we can establish a useful total ordering over them; will base colors on this commit 85d4b30d4795636add23a6a2c47b2068e24dbb74 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 21:18:10 2023 -0400 Update README.md Updated info about import/export, including the new Import Transfer from CSV feature. commit 11a561c1d447e1376e58f970f74d0778e8ed1722 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 20:32:51 2023 -0400 Add text to button commit 562dc2adf60b8569da579b1bb637cea344a7a596 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 20:32:40 2023 -0400 Change to make colors more evenly distributed commit 6b09aad289b83576bdf67882ee3f30b7f1d23619 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 19:27:02 2023 -0400 Implementation 1 commit a9e5f05fd9c0ebcbbb1c3cc86af8f30c92f373c5 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 17:18:45 2023 -0400 Hide parts of transfer menu when Custom transfer selected commit db345bfbb585c826c6f97424c5ee61337c4ffbfd Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 17:18:08 2023 -0400 delete weird whitespace from Cargo.toml commit edcc3528aab3b17aedaf7b3424ece82ad09d591a Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 16:41:58 2023 -0400 First implementation of custom region type commit 9a3a10c8b43e51471f74f79dfbef66b3f69ea0f5 Author: Emilia <contact@emiliaallison.com> Date: Tue Oct 24 16:21:30 2023 -0400 Transfer region no longer copy
2023-12-30 01:39:00 +00:00
2023-06-06 01:33:23 +00:00
}