Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(color): off-by-one for INDEXED_COLORS access #191

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/structs/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Color {
/// In that case, use get_argb_with_theme(&self, theme: &Theme).
pub fn get_argb(&self) -> &str {
if self.indexed.has_value() {
match INDEXED_COLORS.get(self.indexed.get_value().clone() as usize - 1) {
match INDEXED_COLORS.get(self.indexed.get_value().clone() as usize) {
Some(v) => return v,
None => {}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ impl Color {
let indexed = INDEXED_COLORS.iter().position(|&r| r == argb);
match indexed {
Some(v) => {
self.indexed.set_value((v + 1) as u32);
self.indexed.set_value(v as u32);
self.argb.remove_value();
}
None => {
Expand Down Expand Up @@ -309,7 +309,7 @@ mod tests {

let mut obj = Color::default();
obj.set_argb("FFFF8080");
assert_eq!(obj.get_indexed(), &22);
assert_eq!(obj.get_indexed(), &21);
assert_eq!(obj.get_argb(), "FFFF8080");

let mut obj = Color::default();
Expand Down
12 changes: 12 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1400,3 +1400,15 @@ fn issue_188() {
let path = std::path::Path::new("./tests/result_files/issue_188.xlsx");
let _ = umya_spreadsheet::writer::xlsx::write(&book, path);
}

#[test]
fn expect_red_indexed_color() {
let path = std::path::Path::new("./tests/test_files/red_indexed_color.xlsx");
let book = umya_spreadsheet::reader::xlsx::read(path).unwrap();

let cell = book.get_sheet(&0).unwrap().get_cell("A1").unwrap();

let color = cell.get_style().get_font().unwrap().get_color().get_argb();

assert_eq!("FFFF0000", color);
}
Binary file added tests/test_files/red_indexed_color.xlsx
Binary file not shown.