From 10229ca8b5a377aac6291bc5a2ea1eb5b8a155e5 Mon Sep 17 00:00:00 2001 From: Richard Shiue <71320345+richardshiue@users.noreply.github.com> Date: Tue, 25 Oct 2022 08:35:31 +0800 Subject: [PATCH] fix: avoid overlapping option colors (#1318) --- .../select_type_option.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/select_type_option.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/select_type_option.rs index 06244239b6..7d6ec218b0 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/select_type_option.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/select_type_option.rs @@ -101,7 +101,7 @@ pub trait SelectTypeOptionSharedAction: TypeOptionDataSerializer + Send + Sync { } fn create_option(&self, name: &str) -> SelectOptionPB { - let color = select_option_color_from_index(self.options().len()); + let color = new_select_option_color(self.options()); SelectOptionPB::with_color(name, color) } @@ -215,8 +215,20 @@ pub fn select_type_option_from_field_rev( } } -pub fn select_option_color_from_index(index: usize) -> SelectOptionColorPB { - match index % 8 { +pub fn new_select_option_color(options: &Vec) -> SelectOptionColorPB { + let mut freq: Vec = vec![0; 9]; + + for option in options { + freq[option.color.to_owned() as usize] += 1; + } + + match freq + .into_iter() + .enumerate() + .min_by_key(|(_, v)| *v) + .map(|(idx, _val)| idx) + .unwrap() + { 0 => SelectOptionColorPB::Purple, 1 => SelectOptionColorPB::Pink, 2 => SelectOptionColorPB::LightPink,