Skip to content

Commit 0b50886

Browse files
committed
allow setting background color and font color dynamically
1 parent 198b5f1 commit 0b50886

6 files changed

Lines changed: 116 additions & 13 deletions

File tree

webgpu/background.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,21 @@ def __init__(self, position=(0, 0), width=1, height=0.05):
5858
self._position = position
5959
self._width = width
6060
self._height = height
61+
self._bg_color = (1.0, 1.0, 1.0)
6162
self.uniforms = None
6263

64+
@property
65+
def bg_color(self):
66+
return self._bg_color
67+
68+
@bg_color.setter
69+
def bg_color(self, value):
70+
self._bg_color = tuple(value[:3])
71+
if self.uniforms is not None:
72+
self.uniforms.bg_color = self._bg_color
73+
self.uniforms.update_buffer()
74+
self.set_needs_update()
75+
6376
@property
6477
def position(self):
6578
return self._position
@@ -108,7 +121,7 @@ def update(self, options: RenderOptions):
108121
self.uniforms.position = self.position
109122
self.uniforms.width = self.width
110123
self.uniforms.height = self.height
111-
self.uniforms.bg_color = (1.0, 1.0, 1.0)
124+
self.uniforms.bg_color = self._bg_color
112125
self.uniforms.update_buffer()
113126

114127
def create_render_pipeline(self, options: RenderOptions) -> None:

webgpu/canvas.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010

1111
_TARGET_FPS = 60
1212

13+
_default_clear_color = None
14+
15+
16+
def set_default_clear_color(color):
17+
"""Set the clear color used by Canvas instances created afterwards."""
18+
global _default_clear_color
19+
_default_clear_color = color
1320

14-
# @dataclass
15-
# class _DebounceData:
16-
# t_last_frame: float = 0
17-
# t_last_call: float = 0
18-
# timer: threading.Timer | None = None
19-
# lock: Lock = None
20-
# running: bool = False
21-
# pending: bool = False
2221

2322
class _InstanceDebounce:
2423
"""Descriptor that creates per-instance debounced methods.
@@ -291,8 +290,9 @@ def __init__(self, device, canvas, multisample_count=4):
291290

292291
self.dpr = 1.0 # updated in resize(); kept as attribute for camera uniforms
293292

294-
# Background clear color of the scene (opaque white by default).
295-
self.clear_color = Color(1, 1, 1, 1)
293+
self.clear_color = (
294+
Color(1, 1, 1, 1) if _default_clear_color is None else _default_clear_color
295+
)
296296

297297
self.update_html_canvas(canvas)
298298

webgpu/colormap.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,74 @@ def set_max(self, maxval):
385385

386386

387387
_colormaps = {
388+
"turbo": [
389+
[48, 18, 59],
390+
[57, 42, 115],
391+
[64, 64, 162],
392+
[68, 86, 199],
393+
[71, 110, 230],
394+
[70, 130, 248],
395+
[65, 150, 255],
396+
[53, 171, 248],
397+
[37, 192, 231],
398+
[26, 210, 210],
399+
[24, 224, 189],
400+
[34, 235, 170],
401+
[60, 245, 142],
402+
[89, 251, 115],
403+
[121, 254, 89],
404+
[150, 254, 68],
405+
[175, 250, 55],
406+
[195, 241, 52],
407+
[215, 229, 53],
408+
[231, 215, 57],
409+
[245, 197, 58],
410+
[252, 179, 54],
411+
[254, 158, 47],
412+
[252, 135, 37],
413+
[246, 108, 25],
414+
[237, 85, 16],
415+
[226, 67, 10],
416+
[212, 51, 5],
417+
[193, 35, 2],
418+
[172, 23, 1],
419+
[149, 13, 1],
420+
[122, 4, 3],
421+
],
422+
"rainbow": [
423+
[13, 59, 221],
424+
[11, 76, 222],
425+
[9, 94, 224],
426+
[7, 111, 225],
427+
[5, 128, 227],
428+
[3, 145, 228],
429+
[0, 163, 230],
430+
[4, 170, 214],
431+
[9, 175, 194],
432+
[14, 180, 174],
433+
[19, 184, 154],
434+
[24, 189, 134],
435+
[29, 194, 114],
436+
[51, 198, 96],
437+
[83, 200, 79],
438+
[116, 203, 62],
439+
[148, 205, 44],
440+
[181, 208, 27],
441+
[213, 210, 10],
442+
[234, 207, 0],
443+
[238, 195, 0],
444+
[242, 183, 0],
445+
[245, 171, 0],
446+
[249, 159, 0],
447+
[252, 148, 0],
448+
[254, 135, 1],
449+
[249, 117, 6],
450+
[245, 100, 11],
451+
[240, 83, 16],
452+
[235, 66, 21],
453+
[231, 48, 26],
454+
[226, 31, 31],
455+
],
388456
"viridis": [
389457
[68, 1, 84],
390458
[71, 13, 96],

webgpu/font.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class FontUniforms(UniformBase):
3131
("size_scaling", ct.c_float),
3232
("advance", ct.c_float),
3333
("font_size", ct.c_float),
34-
("padding", ct.c_float * 3),
34+
("color", ct.c_float * 3),
3535
]
3636

3737

@@ -202,6 +202,11 @@ def set_font_size(self, font_size: float):
202202
self.uniforms.size = font_size
203203
self._update()
204204

205+
def set_color(self, color):
206+
"""Set the text color as an (r, g, b) tuple in [0, 1]."""
207+
self.uniforms.color = tuple(color[:3])
208+
self.uniforms.update_buffer()
209+
205210
def update(self):
206211
self.uniforms.update_buffer()
207212

webgpu/labels.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,21 @@ def __init__(
5353
self.buffer = None
5454
self._overlay_buf = None
5555
self.font = None
56+
self._text_color = None # (r,g,b) for the default (non-per-label) path
5657

5758
if colors is not None:
5859
self.fragment_entry_point = "fragmentFontColor"
5960

61+
@property
62+
def text_color(self):
63+
return self._text_color
64+
65+
@text_color.setter
66+
def text_color(self, value):
67+
self._text_color = None if value is None else tuple(value[:3])
68+
if self.font is not None and self._text_color is not None:
69+
self.font.set_color(self._text_color)
70+
6071
def update(self, options: RenderOptions):
6172
n_chars = sum(len(label) for label in self.labels)
6273
n_labels = len(self.labels)
@@ -93,6 +104,8 @@ def update(self, options: RenderOptions):
93104
self.font = Font(options.canvas, self.font_size)
94105
else:
95106
self.font.set_font_size(self.font_size)
107+
if self._text_color is not None:
108+
self.font.set_color(self._text_color)
96109

97110
char_map = self.font.atlas.char_map
98111

webgpu/shaders/font.wgsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ struct FontUniforms {
1010
size_scaling: f32,
1111
advance: f32,
1212
font_size: f32,
13+
color_r: f32,
14+
color_g: f32,
15+
color_b: f32,
1316
};
1417

1518
@group(0) @binding(2) var<uniform> u_font : FontUniforms;
@@ -82,7 +85,8 @@ fn fragmentFont(@location(0) tex_coord: vec2<f32>) -> @location(0) vec4<f32> {
8285
discard;
8386
}
8487

85-
return vec4(0., 0., 0., alpha);
88+
let color = vec3f(u_font.color_r, u_font.color_g, u_font.color_b);
89+
return vec4(color * alpha, alpha);
8690
}
8791

8892
fn fontCalc(char: u32, position: vec4<f32>, vertexId: u32) -> FontFragmentInput {

0 commit comments

Comments
 (0)