@@ -0,0 +1,220 @@ |
| 1 | +//! Color palette tab. |
| 2 | + |
| 3 | +use gartk_core::{Color, Point, Rect}; |
| 4 | +use gartk_render::{fill_rounded_rect, set_color, stroke_rounded_rect}; |
| 5 | + |
| 6 | +use super::{PADDING, PICKER_WIDTH}; |
| 7 | + |
| 8 | +/// Color swatch size. |
| 9 | +const SWATCH_SIZE: u32 = 28; |
| 10 | + |
| 11 | +/// Swatch spacing. |
| 12 | +const SWATCH_SPACING: u32 = 4; |
| 13 | + |
| 14 | +/// Number of columns in the palette grid. |
| 15 | +const COLUMNS: usize = 9; |
| 16 | + |
| 17 | +/// Preset colors (matching the 1-9 keyboard shortcuts). |
| 18 | +const PRESET_COLORS: [Color; 9] = [ |
| 19 | + Color::new(1.0, 0.4, 0.0, 1.0), // 1: Orange (default) |
| 20 | + Color::new(1.0, 0.0, 0.0, 1.0), // 2: Red |
| 21 | + Color::new(0.0, 1.0, 0.0, 1.0), // 3: Green |
| 22 | + Color::new(0.0, 0.5, 1.0, 1.0), // 4: Blue |
| 23 | + Color::new(1.0, 1.0, 0.0, 1.0), // 5: Yellow |
| 24 | + Color::new(1.0, 0.0, 1.0, 1.0), // 6: Magenta |
| 25 | + Color::new(0.0, 1.0, 1.0, 1.0), // 7: Cyan |
| 26 | + Color::new(1.0, 1.0, 1.0, 1.0), // 8: White |
| 27 | + Color::new(0.0, 0.0, 0.0, 1.0), // 9: Black |
| 28 | +]; |
| 29 | + |
| 30 | +/// Extended palette colors. |
| 31 | +const EXTENDED_PALETTE: [Color; 36] = [ |
| 32 | + // Row 1: Reds |
| 33 | + Color::new(1.0, 0.8, 0.8, 1.0), |
| 34 | + Color::new(1.0, 0.6, 0.6, 1.0), |
| 35 | + Color::new(1.0, 0.4, 0.4, 1.0), |
| 36 | + Color::new(1.0, 0.2, 0.2, 1.0), |
| 37 | + Color::new(0.8, 0.0, 0.0, 1.0), |
| 38 | + Color::new(0.6, 0.0, 0.0, 1.0), |
| 39 | + Color::new(0.4, 0.0, 0.0, 1.0), |
| 40 | + Color::new(0.3, 0.0, 0.0, 1.0), |
| 41 | + Color::new(0.2, 0.0, 0.0, 1.0), |
| 42 | + // Row 2: Oranges/Yellows |
| 43 | + Color::new(1.0, 0.9, 0.7, 1.0), |
| 44 | + Color::new(1.0, 0.8, 0.4, 1.0), |
| 45 | + Color::new(1.0, 0.6, 0.2, 1.0), |
| 46 | + Color::new(1.0, 0.5, 0.0, 1.0), |
| 47 | + Color::new(0.9, 0.7, 0.0, 1.0), |
| 48 | + Color::new(0.8, 0.8, 0.0, 1.0), |
| 49 | + Color::new(0.6, 0.6, 0.0, 1.0), |
| 50 | + Color::new(0.4, 0.4, 0.0, 1.0), |
| 51 | + Color::new(0.3, 0.3, 0.0, 1.0), |
| 52 | + // Row 3: Greens |
| 53 | + Color::new(0.8, 1.0, 0.8, 1.0), |
| 54 | + Color::new(0.6, 1.0, 0.6, 1.0), |
| 55 | + Color::new(0.4, 1.0, 0.4, 1.0), |
| 56 | + Color::new(0.0, 0.9, 0.0, 1.0), |
| 57 | + Color::new(0.0, 0.7, 0.0, 1.0), |
| 58 | + Color::new(0.0, 0.5, 0.0, 1.0), |
| 59 | + Color::new(0.0, 0.4, 0.0, 1.0), |
| 60 | + Color::new(0.0, 0.3, 0.0, 1.0), |
| 61 | + Color::new(0.0, 0.2, 0.0, 1.0), |
| 62 | + // Row 4: Blues/Purples |
| 63 | + Color::new(0.8, 0.8, 1.0, 1.0), |
| 64 | + Color::new(0.6, 0.6, 1.0, 1.0), |
| 65 | + Color::new(0.4, 0.4, 1.0, 1.0), |
| 66 | + Color::new(0.2, 0.2, 1.0, 1.0), |
| 67 | + Color::new(0.0, 0.0, 0.8, 1.0), |
| 68 | + Color::new(0.4, 0.0, 0.8, 1.0), |
| 69 | + Color::new(0.6, 0.0, 0.8, 1.0), |
| 70 | + Color::new(0.8, 0.0, 0.8, 1.0), |
| 71 | + Color::new(0.4, 0.0, 0.4, 1.0), |
| 72 | +]; |
| 73 | + |
| 74 | +/// Get the content rect for the palette tab. |
| 75 | +fn content_rect() -> Rect { |
| 76 | + Rect::new( |
| 77 | + PADDING as i32, |
| 78 | + 60, // After tab bar |
| 79 | + PICKER_WIDTH - PADDING * 2, |
| 80 | + 250, |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +/// Get rect for a swatch at given row and column. |
| 85 | +fn swatch_rect(row: usize, col: usize, content: Rect) -> Rect { |
| 86 | + Rect::new( |
| 87 | + content.x + (col as i32 * (SWATCH_SIZE as i32 + SWATCH_SPACING as i32)), |
| 88 | + content.y + (row as i32 * (SWATCH_SIZE as i32 + SWATCH_SPACING as i32)), |
| 89 | + SWATCH_SIZE, |
| 90 | + SWATCH_SIZE, |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +/// Handle click in palette tab. |
| 95 | +pub fn handle_click(local_pos: Point, recent: &[Color]) -> Option<Color> { |
| 96 | + let content = content_rect(); |
| 97 | + |
| 98 | + // Check preset colors (row 0) |
| 99 | + for (col, color) in PRESET_COLORS.iter().enumerate() { |
| 100 | + let rect = swatch_rect(0, col, content); |
| 101 | + if rect.contains_point(local_pos) { |
| 102 | + return Some(*color); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Check extended palette (rows 1-4) |
| 107 | + for (i, color) in EXTENDED_PALETTE.iter().enumerate() { |
| 108 | + let row = 2 + (i / COLUMNS); |
| 109 | + let col = i % COLUMNS; |
| 110 | + let rect = swatch_rect(row, col, content); |
| 111 | + if rect.contains_point(local_pos) { |
| 112 | + return Some(*color); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Check recent colors (last row) |
| 117 | + let recent_row = 7; |
| 118 | + for (col, color) in recent.iter().enumerate().take(8) { |
| 119 | + let rect = swatch_rect(recent_row, col, content); |
| 120 | + if rect.contains_point(local_pos) { |
| 121 | + return Some(*color); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + None |
| 126 | +} |
| 127 | + |
| 128 | +/// Draw the palette tab content. |
| 129 | +pub fn draw(ctx: &cairo::Context, _content_rect: Rect, recent: &[Color]) -> anyhow::Result<()> { |
| 130 | + let content = content_rect(); |
| 131 | + |
| 132 | + // Draw section label for presets |
| 133 | + set_color(ctx, Color::new(0.7, 0.7, 0.7, 1.0)); |
| 134 | + ctx.select_font_face("sans-serif", cairo::FontSlant::Normal, cairo::FontWeight::Normal); |
| 135 | + ctx.set_font_size(11.0); |
| 136 | + ctx.move_to(content.x as f64, (content.y - 4) as f64); |
| 137 | + ctx.show_text("Presets (1-9)")?; |
| 138 | + |
| 139 | + // Draw preset colors |
| 140 | + for (col, color) in PRESET_COLORS.iter().enumerate() { |
| 141 | + let rect = swatch_rect(0, col, content); |
| 142 | + draw_swatch(ctx, rect, *color)?; |
| 143 | + |
| 144 | + // Draw key number |
| 145 | + set_color(ctx, Color::WHITE); |
| 146 | + ctx.set_font_size(10.0); |
| 147 | + ctx.move_to((rect.x + 2) as f64, (rect.y + rect.height as i32 - 3) as f64); |
| 148 | + ctx.show_text(&format!("{}", col + 1))?; |
| 149 | + } |
| 150 | + |
| 151 | + // Draw section label for extended palette |
| 152 | + let extended_y = content.y + (SWATCH_SIZE as i32 + SWATCH_SPACING as i32) + 8; |
| 153 | + set_color(ctx, Color::new(0.7, 0.7, 0.7, 1.0)); |
| 154 | + ctx.set_font_size(11.0); |
| 155 | + ctx.move_to(content.x as f64, extended_y as f64); |
| 156 | + ctx.show_text("Extended")?; |
| 157 | + |
| 158 | + // Draw extended palette |
| 159 | + for (i, color) in EXTENDED_PALETTE.iter().enumerate() { |
| 160 | + let row = 2 + (i / COLUMNS); |
| 161 | + let col = i % COLUMNS; |
| 162 | + let rect = swatch_rect(row, col, content); |
| 163 | + draw_swatch(ctx, rect, *color)?; |
| 164 | + } |
| 165 | + |
| 166 | + // Draw section label for recent colors |
| 167 | + let recent_y = swatch_rect(6, 0, content).y + SWATCH_SIZE as i32 + 8; |
| 168 | + set_color(ctx, Color::new(0.7, 0.7, 0.7, 1.0)); |
| 169 | + ctx.set_font_size(11.0); |
| 170 | + ctx.move_to(content.x as f64, recent_y as f64); |
| 171 | + ctx.show_text("Recent")?; |
| 172 | + |
| 173 | + // Draw recent colors |
| 174 | + let recent_row = 7; |
| 175 | + for col in 0..8 { |
| 176 | + let rect = swatch_rect(recent_row, col, content); |
| 177 | + if col < recent.len() { |
| 178 | + draw_swatch(ctx, rect, recent[col])?; |
| 179 | + } else { |
| 180 | + // Draw empty swatch placeholder |
| 181 | + fill_rounded_rect(ctx, rect, 4.0, Color::new(0.2, 0.2, 0.2, 1.0)); |
| 182 | + stroke_rounded_rect(ctx, rect, 4.0, Color::new(0.3, 0.3, 0.3, 1.0), 1.0); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + Ok(()) |
| 187 | +} |
| 188 | + |
| 189 | +/// Draw a color swatch. |
| 190 | +fn draw_swatch(ctx: &cairo::Context, rect: Rect, color: Color) -> anyhow::Result<()> { |
| 191 | + // Draw checkerboard for alpha |
| 192 | + if color.a < 1.0 { |
| 193 | + let check_size = 4.0; |
| 194 | + for y in 0..((rect.height as f64 / check_size) as i32) { |
| 195 | + for x in 0..((rect.width as f64 / check_size) as i32) { |
| 196 | + let is_light = (x + y) % 2 == 0; |
| 197 | + set_color( |
| 198 | + ctx, |
| 199 | + if is_light { |
| 200 | + Color::new(0.6, 0.6, 0.6, 1.0) |
| 201 | + } else { |
| 202 | + Color::new(0.4, 0.4, 0.4, 1.0) |
| 203 | + }, |
| 204 | + ); |
| 205 | + ctx.rectangle( |
| 206 | + rect.x as f64 + x as f64 * check_size, |
| 207 | + rect.y as f64 + y as f64 * check_size, |
| 208 | + check_size, |
| 209 | + check_size, |
| 210 | + ); |
| 211 | + ctx.fill()?; |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + fill_rounded_rect(ctx, rect, 4.0, color); |
| 217 | + stroke_rounded_rect(ctx, rect, 4.0, Color::new(0.4, 0.4, 0.4, 1.0), 1.0); |
| 218 | + |
| 219 | + Ok(()) |
| 220 | +} |