Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
DESKTOP-U434MT0\hiro committed Aug 16, 2024
1 parent b3175f6 commit fbe9bb9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
68 changes: 46 additions & 22 deletions src/helper/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ pub struct HlsColor {
pub s: f64,
}

const HLSMAX: f64 = 255f64;
#[derive(Default, Debug, Clone, PartialEq, PartialOrd)]
pub struct MsHlsColor {
pub h: i32,
pub l: i32,
pub s: i32,
}

const RGBMAX: f64 = 255f64;
const HLSMAX: f64 = 240f64;

pub fn calc_tint(rgb: &str, tint: &f64) -> String {
let mut hls = convert_rgb_to_hls(rgb);
let calculate_final_lum_value = calculate_final_lum_value(tint, &(hls.l * HLSMAX)) / HLSMAX;
hls.l = calculate_final_lum_value;
convert_hls_to_rgb(&hls)
let mut ms_hls = convert_rgb_to_ms_hls(rgb);
let calculate_final_lum_value = calculate_final_lum_value(tint, &(ms_hls.l as f64));
ms_hls.l = calculate_final_lum_value;
convert_ms_hls_to_rgb(&ms_hls)
}

pub fn calculate_final_lum_value(tint: &f64, lum: &f64) -> f64 {
pub fn calculate_final_lum_value(tint: &f64, lum: &f64) -> i32 {
let mut lum1 = 0.0;

if tint < &0.0 {
Expand All @@ -27,7 +35,7 @@ pub fn calculate_final_lum_value(tint: &f64, lum: &f64) -> f64 {
lum1 = lum * (1.0 - tint) + (HLSMAX - HLSMAX * (1.0 - tint));
}

return lum1;
return to_i32(lum1);
}

pub fn split_rgb(rgb: &str) -> (i32, i32, i32) {
Expand All @@ -41,17 +49,28 @@ pub fn split_rgb(rgb: &str) -> (i32, i32, i32) {
}

pub fn join_rgb(r: &i32, g: &i32, b: &i32) -> String {
format!("{:X}{:X}{:X}", r, g, b)
format!("{:02X}{:02X}{:02X}", r, g, b)
}

pub fn convert_rgb_to_ms_hls(rgb: &str) -> MsHlsColor {
let hls = convert_rgb_to_hls(rgb);

let mut ms_hls = MsHlsColor::default();
ms_hls.h = to_i32(hls.h * self::HLSMAX);
ms_hls.l = to_i32(hls.l * self::HLSMAX);
ms_hls.s = to_i32(hls.s * self::HLSMAX);

ms_hls
}

pub fn convert_rgb_to_hls(rgb: &str) -> HlsColor {
let mut hls = HlsColor::default();

let (r_i, g_i, b_i) = split_rgb(rgb);

let r = r_i as f64 / HLSMAX;
let g = g_i as f64 / HLSMAX;
let b = b_i as f64 / HLSMAX;
let r = r_i as f64 / RGBMAX;
let g = g_i as f64 / RGBMAX;
let b = b_i as f64 / RGBMAX;

let mut min = r;
if min > g {
Expand Down Expand Up @@ -107,9 +126,17 @@ pub fn convert_rgb_to_hls(rgb: &str) -> HlsColor {
return hls;
}

pub fn convert_ms_hls_to_rgb(ms_hls: &MsHlsColor) -> String {
let mut hls = HlsColor::default();
hls.h = ms_hls.h as f64 / self::HLSMAX;
hls.l = ms_hls.l as f64 / self::HLSMAX;
hls.s = ms_hls.s as f64 / self::HLSMAX;
convert_hls_to_rgb(&hls)
}

pub fn convert_hls_to_rgb(hls: &HlsColor) -> String {
if hls.s == 0.0 {
let rtn_l = (hls.l * HLSMAX) as i32;
let rtn_l = to_i32(hls.l * RGBMAX);
return join_rgb(&rtn_l, &rtn_l, &rtn_l);
}

Expand All @@ -128,21 +155,18 @@ pub fn convert_hls_to_rgb(hls: &HlsColor) -> String {
let t_b = h - (1.0 / 3.0);
let b = set_color(&t1, &t2, &t_b);

let rtn_r = to_i32(r * HLSMAX);
let rtn_g = to_i32(g * HLSMAX);
let rtn_b = to_i32(b * HLSMAX);
let rtn_r = to_i32(r * RGBMAX);
let rtn_g = to_i32(g * RGBMAX);
let rtn_b = to_i32(b * RGBMAX);
join_rgb(&rtn_r, &rtn_g, &rtn_b)
}

pub fn set_color(t1: &f64, t2: &f64, t3: &f64) -> f64 {
let mut t1 = t1.clone();
let mut t2 = t2.clone();
let mut t3 = t3.clone();
if t3 < 0.0 {
t3 += 1.0;
}
if t3 > 1.0 {
t3 -= 1.0;
let mut t3 = t3 % 1.0;
if t3 < 1.0 {
t3 = 1.0 + t3;
}

let mut color: f64 = 0.0;
Expand All @@ -160,7 +184,7 @@ pub fn set_color(t1: &f64, t2: &f64, t3: &f64) -> f64 {
}

fn to_i32(num: f64) -> i32 {
num.floor() as i32
num.round() as i32
}

#[cfg(test)]
Expand Down
12 changes: 12 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,18 @@ fn issue_184_2() {
let mut book = umya_spreadsheet::reader::xlsx::read(path).unwrap();

return;

let color = book
.get_sheet(&0)
.unwrap()
.get_cell("C1")
.unwrap()
.get_style()
.get_background_color()
.unwrap()
.get_argb_with_theme(book.get_theme());
assert_eq!(color, "E8E8E8");

let color = book
.get_sheet(&0)
.unwrap()
Expand Down

0 comments on commit fbe9bb9

Please sign in to comment.