ltk/widget/tab_strip/
icons.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

use std::cell::RefCell;
use std::collections::HashMap;

use crate::types::Color;
use crate::wallpaper::ImageData;

/// Rasterised at twice the drawn size so icons stay sharp at scale 2.
const RASTER_PX: u32 = 48;

thread_local!
{
	static CACHE: RefCell<HashMap<( String, [ u8; 4 ] ), Option<ImageData>>> = RefCell::new( HashMap::new() );
}

/// The theme's symbolic icon `name` in `tint`, kept so the same pixels are handed out every frame.
pub( crate ) fn tinted( name: &str, tint: Color ) -> Option<ImageData>
{
	let key = ( name.to_string(), [ channel( tint.r ), channel( tint.g ), channel( tint.b ), channel( tint.a ) ] );
	CACHE.with( |cache|
	{
		cache.borrow_mut()
			.entry( key )
			.or_insert_with( || crate::chassis::theme_icon_tinted( name, RASTER_PX, tint ) )
			.clone()
	} )
}

fn channel( v: f32 ) -> u8
{
	( v.clamp( 0.0, 1.0 ) * 255.0 ) as u8
}