1
Fork 0
mirror of https://github.com/RGBCube/color.v synced 2025-08-01 10:27:45 +00:00

Make a factory method for PaintBrush (validate styles)

This commit is contained in:
RGBCube 2022-11-18 21:30:28 +03:00
parent cae034cfb3
commit 712324f893
3 changed files with 36 additions and 10 deletions

View file

@ -19,11 +19,11 @@ color.bold.cprintln('Hello World')
```v
import color
brush := color.PaintBrush{
brush := color.new_brush(
fg: color.rgb(0, 0, 0)!
bg: color.hex(0xffffff)!
styles: [color.bold, color.underline, color.italic]
}
style: [color.bold, color.underline, color.italic]
)!
brush.cprintln('Hello World')
```

View file

@ -3,11 +3,11 @@ module main
import color
fn main() {
p := color.PaintBrush{
p := color.new_brush(
fg: color.rgb(0, 0, 0)!
bg: color.hex(0xffffff)!
styles: [color.bold, color.underline, color.italic]
}
style: [color.bold, color.underline, color.italic]
)!
p.cprintln('Hello World')
}

View file

@ -1,10 +1,36 @@
module color
[noinit]
pub struct PaintBrush {
pub:
fg ?Color
bg ?Color
styles []Style
style []Style
}
[params]
pub struct PaintBrushParams {
fg ?Color
bg ?Color
style []Style
}
pub fn new_brush(p PaintBrushParams) !PaintBrush {
mut count := map[Style]int{}
for style in p.style {
count[style]++
if count[style] > 1 {
return error('Multiple of the same style was provided')
}
}
return PaintBrush{
fg: p.fg
bg: p.bg
style: p.style
}
}
pub fn (p &PaintBrush) render(msg string) string {
@ -20,7 +46,7 @@ pub fn (p &PaintBrush) render(msg string) string {
if bg := p.bg {
result = bg.render(result)
}
for style in p.styles {
for style in p.style {
result = style.render(result)
}