1
Fork 0
mirror of https://github.com/RGBCube/color.v synced 2025-07-30 17:37:45 +00:00

Initial commit

This commit is contained in:
RGBCube 2022-11-18 19:41:26 +03:00
parent 0cb83fd1a3
commit 992c9da2e9
10 changed files with 277 additions and 0 deletions

9
.editorconfig Normal file
View file

@ -0,0 +1,9 @@
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.v]
indent_style = tab
indent_size = 4

7
.gitattributes vendored Normal file
View file

@ -0,0 +1,7 @@
* text=auto eol=lf
*.bat eol=crlf
**/*.v linguist-language=V
**/*.vv linguist-language=V
**/*.vsh linguist-language=V
**/v.mod linguist-language=V

14
.gitignore vendored Normal file
View file

@ -0,0 +1,14 @@
# Binaries for programs and plugins
main
VColor
*.exe
*.exe~
*.so
*.dylib
*.dll
# Ignore common editor/system specific metadata
.DS_Store
.idea/
.vscode/
*.iml

28
README.md Normal file
View file

@ -0,0 +1,28 @@
# v-color
An easier way to print color to the terminal
# Example
## Basic
```v
import color
println(color.red.apply('Hello World'))
println(color.bold.apply('Hello World'))
```
## Advanced
```v
import color
p := color.PaintBrush{
fg: color.rgb(0, 0, 0)!,
bg: color.hex(0xffffff)!,
styles: [color.bold, color.underline, color.italic]
}
print(p.apply('Hello World'))
```

85
color/basic_color.v Normal file
View file

@ -0,0 +1,85 @@
module color
import term
pub const (
black = BasicColor.black
red = BasicColor.red
green = BasicColor.green
yellow = BasicColor.yellow
blue = BasicColor.blue
magenta = BasicColor.magenta
cyan = BasicColor.cyan
white = BasicColor.white
bright_black = BasicColor.bright_black
bright_red = BasicColor.bright_red
bright_green = BasicColor.bright_green
bright_yellow = BasicColor.bright_yellow
bright_blue = BasicColor.bright_blue
bright_magenta = BasicColor.bright_magenta
bright_cyan = BasicColor.bright_cyan
bright_white = BasicColor.bright_white
)
enum BasicColor {
black
red
green
yellow
blue
magenta
cyan
white
bright_black
bright_red
bright_green
bright_yellow
bright_blue
bright_magenta
bright_cyan
bright_white
}
pub fn (c BasicColor) apply(msg string) string {
func := match c {
.black { term.black }
.red { term.red }
.green { term.green }
.yellow { term.yellow }
.blue { term.blue }
.magenta { term.magenta }
.cyan { term.cyan }
.white { term.white }
.bright_black { term.bright_black }
.bright_red { term.bright_red }
.bright_green { term.bright_green }
.bright_yellow { term.bright_yellow }
.bright_blue { term.bright_blue }
.bright_magenta { term.bright_magenta }
.bright_cyan { term.bright_cyan }
.bright_white { term.bright_white }
}
return func(msg)
}
pub fn (c BasicColor) apply_bg(msg string) string {
func := match c {
.black { term.bg_black }
.red { term.bg_red }
.green { term.bg_green }
.yellow { term.bg_yellow }
.blue { term.bg_blue }
.magenta { term.bg_magenta }
.cyan { term.bg_cyan }
.white { term.bg_white }
.bright_black { term.bright_bg_black }
.bright_red { term.bright_bg_red }
.bright_green { term.bright_bg_green }
.bright_yellow { term.bright_bg_yellow }
.bright_blue { term.bright_bg_blue }
.bright_magenta { term.bright_bg_magenta }
.bright_cyan { term.bright_bg_cyan }
.bright_white { term.bright_bg_white }
}
return func(msg)
}

21
color/color.v Normal file
View file

@ -0,0 +1,21 @@
module color
type Color = BasicColor | NoColor | TrueColor
const no_color = NoColor{}
struct NoColor {}
fn (c Color) apply(msg string) string {
return match c {
NoColor { msg }
else { c.apply(msg) }
}
}
fn (c Color) apply_bg(msg string) string {
return match c {
NoColor { msg }
else { c.apply_bg(msg) }
}
}

27
color/paintbrush.v Normal file
View file

@ -0,0 +1,27 @@
module color
import term
import datatypes
const can_show_color = term.can_show_color_on_stdout()
pub struct PaintBrush {
pub:
fg Color = no_color
bg Color = no_color
styles []Style
}
pub fn (p &PaintBrush) apply(msg string) string {
if !color.can_show_color {
return msg
}
mut result := p.bg.apply(p.fg.apply(msg))
for style in p.styles {
result = style.apply(result)
}
return result
}

45
color/style.v Normal file
View file

@ -0,0 +1,45 @@
module color
import term
pub const (
reset = Style.reset
bold = Style.bold
dim = Style.dim
italic = Style.italic
underline = Style.underline
// slow_blink = Style.slow_blink
// rapid_blink = Style.rapid_blink
inverse = Style.inverse
hidden = Style.hidden
strikethrough = Style.strikethrough
)
enum Style {
reset
bold
dim
italic
underline
// slow_blink
// rapid_blink
inverse
hidden
strikethrough
}
pub fn (s Style) apply(msg string) string {
func := match s {
.reset { term.reset }
.bold { term.bold }
.dim { term.dim }
.italic { term.italic }
.underline { term.underline }
//.slow_blink { term.slow_blink }
//.rapid_blink { term.rapid_blink }
.inverse { term.inverse }
.hidden { term.hidden }
.strikethrough { term.strikethrough }
}
return func(msg)
}

34
color/true_color.v Normal file
View file

@ -0,0 +1,34 @@
module color
import term
[noinit]
struct TrueColor {
pub:
r int [required]
g int [required]
b int [required]
}
pub fn rgb(r int, g int, b int) !TrueColor {
if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 {
return error('Red, green and blue must each be between 0 and 255')
}
return TrueColor{
r: r
g: g
b: b
}
}
pub fn hex(hex int) !TrueColor {
return rgb(hex >> 16, hex >> 8 & 0xFF, hex & 0xFF)!
}
pub fn (c TrueColor) apply(msg string) string {
return term.rgb(c.r, c.g, c.b, msg)
}
pub fn (c TrueColor) apply_bg(msg string) string {
return term.bg_rgb(c.r, c.g, c.b, msg)
}

7
v.mod Normal file
View file

@ -0,0 +1,7 @@
Module {
name: 'color'
description: 'An easier way to print color to the terminal'
version: '1.0.0'
license: 'MIT'
dependencies: []
}