Skip to content

Commit a201013

Browse files
committed
gg, examples: add expanding_rect.v (modeled after the example from https://love2d.org/wiki/love), add generic helpers gg.frgb/3 gg.frgba/4
1 parent 8bbd72d commit a201013

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

examples/gg/expanding_rect.v

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import gg
2+
3+
mut r := &gg.Rect{20, 20, 60, 20}
4+
gg.start(
5+
update_fn: fn [mut r] (dt f32, c &gg.Context) {
6+
r.width++
7+
r.height++
8+
}
9+
frame_fn: fn [mut r] (c &gg.Context) {
10+
c.begin()
11+
c.draw_rect_filled(r.x, r.y, r.width, r.height, gg.frgb(0.0, 0.4, 0.4))
12+
c.end()
13+
}
14+
)

vlib/gg/color.v

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,25 @@ pub fn hex(color i32) Color {
130130
}
131131
}
132132

133-
// rgb builds a Color instance from given r, g, b values
133+
// frgb builds a Color instance from the given floating point values (between 0.0 and 1.0) r, g, b
134+
@[inline]
135+
pub fn frgb[T](r T, g T, b T) Color {
136+
return frgba(r, g, b, 1.0)
137+
}
138+
139+
// frgba builds a Color instance from the given floating point values (between 0.0 and 1.0) r, g, b, a
140+
@[inline]
141+
pub fn frgba[T](r T, g T, b T, a T) Color {
142+
return Color{
143+
r: u8(r * 255.0)
144+
g: u8(g * 255.0)
145+
b: u8(b * 255.0)
146+
a: u8(a * 255.0)
147+
}
148+
}
149+
150+
// rgb builds a Color instance from the given r, g, b u8 values
151+
@[inline]
134152
pub fn rgb(r u8, g u8, b u8) Color {
135153
return Color{
136154
r: r
@@ -139,7 +157,8 @@ pub fn rgb(r u8, g u8, b u8) Color {
139157
}
140158
}
141159

142-
// rgba builds a Color instance from given r, g, b, a values
160+
// rgba builds a Color instance from the given r, g, b, a u8 values
161+
@[inline]
143162
pub fn rgba(r u8, g u8, b u8, a u8) Color {
144163
return Color{
145164
r: r

0 commit comments

Comments
 (0)