修正Swatch Scheme在生成包含原始颜色时的定位错乱问题。

This commit is contained in:
徐涛 2025-02-07 22:52:01 +08:00
parent 505af1c67e
commit 131c43c5cf
2 changed files with 9 additions and 11 deletions

View File

@ -24,28 +24,26 @@ impl Swatch {
} }
} }
fn find_interval(&self) -> (usize, usize) { fn find_interval(&self) -> usize {
if !self.include_primary { if !self.include_primary {
return (0, 0); return 0;
} }
if self.primary_key.l == self.min_key { if self.primary_key.l == self.min_key {
return (0, 1); return 0;
} }
if self.primary_key.l == self.max_key { if self.primary_key.l == self.max_key {
return (self.color_amount - 2, self.color_amount - 1); return self.color_amount - 1;
} }
let step = (self.max_key - self.min_key) / (self.color_amount - 1) as f32; let step = (self.max_key - self.min_key) / (self.color_amount - 1) as f32;
let index = ((self.primary_key.l - self.min_key) / step) as usize; ((self.primary_key.l - self.min_key) / step).ceil() as usize
(index, index + 1)
} }
pub fn swatch(&self) -> Vec<Oklch> { pub fn swatch(&self) -> Vec<Oklch> {
let mut swatch = Vec::new(); let mut swatch = Vec::new();
if self.include_primary { if self.include_primary {
let (_, primary_index) = self.find_interval(); let primary_index = self.find_interval();
if primary_index > 0 { if primary_index > 0 {
let step = (self.max_key - self.min_key) / primary_index as f32; let step = (self.primary_key.l - self.min_key) / (primary_index - 1) as f32;
for i in 0..primary_index { for i in 0..primary_index {
let lightness = self.min_key + step * i as f32; let lightness = self.min_key + step * i as f32;
swatch.push(Oklch { swatch.push(Oklch {
@ -55,8 +53,8 @@ impl Swatch {
} }
} }
if primary_index < self.color_amount - 1 { if primary_index < self.color_amount - 1 {
let step = let step = (self.max_key - self.primary_key.l)
(self.max_key - self.min_key) / (self.color_amount - primary_index) as f32; / (self.color_amount - primary_index - 1) as f32;
for i in primary_index..self.color_amount { for i in primary_index..self.color_amount {
let lightness = self.min_key + step * i as f32; let lightness = self.min_key + step * i as f32;
swatch.push(Oklch { swatch.push(Oklch {