aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/gx
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/vlib/gx
downloadcli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/vlib/gx')
-rw-r--r--v_windows/v/vlib/gx/color.v234
-rw-r--r--v_windows/v/vlib/gx/color_test.v63
-rw-r--r--v_windows/v/vlib/gx/image.v14
-rw-r--r--v_windows/v/vlib/gx/text.c.v18
-rw-r--r--v_windows/v/vlib/gx/text.v20
5 files changed, 349 insertions, 0 deletions
diff --git a/v_windows/v/vlib/gx/color.v b/v_windows/v/vlib/gx/color.v
new file mode 100644
index 0000000..e50d932
--- /dev/null
+++ b/v_windows/v/vlib/gx/color.v
@@ -0,0 +1,234 @@
+module gx
+
+pub const (
+ blue = Color{
+ r: 0
+ g: 0
+ b: 255
+ }
+ red = Color{
+ r: 255
+ g: 0
+ b: 0
+ }
+ green = Color{
+ r: 0
+ g: 255
+ b: 0
+ }
+ yellow = Color{
+ r: 255
+ g: 255
+ b: 0
+ }
+ orange = Color{
+ r: 255
+ g: 165
+ b: 0
+ }
+ purple = Color{
+ r: 128
+ g: 0
+ b: 128
+ }
+ black = Color{
+ r: 0
+ g: 0
+ b: 0
+ }
+ gray = Color{
+ r: 128
+ g: 128
+ b: 128
+ }
+ indigo = Color{
+ r: 75
+ g: 0
+ b: 130
+ }
+ pink = Color{
+ r: 255
+ g: 192
+ b: 203
+ }
+ violet = Color{
+ r: 238
+ g: 130
+ b: 238
+ }
+ white = Color{
+ r: 255
+ g: 255
+ b: 255
+ }
+ dark_blue = Color{
+ r: 0
+ g: 0
+ b: 139
+ }
+ dark_gray = Color{
+ r: 169
+ g: 169
+ b: 169
+ }
+ dark_green = Color{
+ r: 0
+ g: 100
+ b: 0
+ }
+ dark_red = Color{
+ r: 139
+ g: 0
+ b: 0
+ }
+ light_blue = Color{
+ r: 173
+ g: 216
+ b: 230
+ }
+ light_gray = Color{
+ r: 211
+ g: 211
+ b: 211
+ }
+ light_green = Color{
+ r: 144
+ g: 238
+ b: 144
+ }
+ light_red = Color{
+ r: 255
+ g: 204
+ b: 203
+ }
+)
+
+// Color represents a 32 bit color value in sRGB format
+pub struct Color {
+pub mut:
+ r byte
+ g byte
+ b byte
+ a byte = 255
+}
+
+// hex takes in a 32 bit integer and splits it into 4 byte values
+pub fn hex(color int) Color {
+ return Color{
+ r: byte((color >> 24) & 0xFF)
+ g: byte((color >> 16) & 0xFF)
+ b: byte((color >> 8) & 0xFF)
+ a: byte(color & 0xFF)
+ }
+}
+
+pub fn rgb(r byte, g byte, b byte) Color {
+ return Color{
+ r: r
+ g: g
+ b: b
+ }
+}
+
+pub fn rgba(r byte, g byte, b byte, a byte) Color {
+ return Color{
+ r: r
+ g: g
+ b: b
+ a: a
+ }
+}
+
+pub fn (c Color) + (c2 Color) Color {
+ return Color{
+ r: c.r + c2.r
+ g: c.g + c2.g
+ b: c.b + c2.b
+ a: c.b + c2.a
+ }
+}
+
+pub fn (c Color) - (c2 Color) Color {
+ return Color{
+ r: c.r - c2.r
+ g: c.g - c2.g
+ b: c.b - c2.b
+ a: c.b - c2.a
+ }
+}
+
+pub fn (c Color) * (c2 Color) Color {
+ return Color{
+ r: c.r * c2.r
+ g: c.g * c2.g
+ b: c.b * c2.b
+ a: c.b * c2.a
+ }
+}
+
+pub fn (c Color) / (c2 Color) Color {
+ return Color{
+ r: c.r / c2.r
+ g: c.g / c2.g
+ b: c.b / c2.b
+ a: c.b / c2.a
+ }
+}
+
+pub fn (c Color) eq(c2 Color) bool {
+ return c.r == c2.r && c.g == c2.g && c.b == c2.b && c.a == c2.a
+}
+
+pub fn (c Color) str() string {
+ return 'Color{$c.r, $c.g, $c.b, $c.a}'
+}
+
+// rgba8 - convert a color value to an int in the RGBA8 order.
+// see https://developer.apple.com/documentation/coreimage/ciformat
+[inline]
+pub fn (c Color) rgba8() int {
+ return (int(c.r) << 24) + (int(c.g) << 16) + (int(c.b) << 8) + int(c.a)
+}
+
+// bgra8 - convert a color value to an int in the BGRA8 order.
+// see https://developer.apple.com/documentation/coreimage/ciformat
+[inline]
+pub fn (c Color) bgra8() int {
+ return (int(c.b) << 24) + (int(c.g) << 16) + (int(c.r) << 8) + int(c.a)
+}
+
+// abgr8 - convert a color value to an int in the ABGR8 order.
+// see https://developer.apple.com/documentation/coreimage/ciformat
+[inline]
+pub fn (c Color) abgr8() int {
+ return (int(c.a) << 24) + (int(c.b) << 16) + (int(c.g) << 8) + int(c.r)
+}
+
+const (
+ string_colors = {
+ 'blue': blue
+ 'red': red
+ 'green': green
+ 'yellow': yellow
+ 'orange': orange
+ 'purple': purple
+ 'black': black
+ 'gray': gray
+ 'indigo': indigo
+ 'pink': pink
+ 'violet': violet
+ 'white': white
+ 'dark_blue': dark_blue
+ 'dark_gray': dark_gray
+ 'dark_green': dark_green
+ 'dark_red': dark_red
+ 'light_blue': light_blue
+ 'light_gray': light_gray
+ 'light_green': light_green
+ 'light_red': light_red
+ }
+)
+
+pub fn color_from_string(s string) Color {
+ return gx.string_colors[s]
+}
diff --git a/v_windows/v/vlib/gx/color_test.v b/v_windows/v/vlib/gx/color_test.v
new file mode 100644
index 0000000..533a41d
--- /dev/null
+++ b/v_windows/v/vlib/gx/color_test.v
@@ -0,0 +1,63 @@
+import gx
+
+fn test_hex() {
+ // valid colors
+ a := gx.hex(0x6c5ce7ff)
+ b := gx.rgba(108, 92, 231, 255)
+ assert a.eq(b)
+ // doesn't give right value with short hex value
+ short := gx.hex(0xfff)
+ assert !short.eq(gx.white)
+}
+
+fn test_add() {
+ a := gx.rgba(100, 100, 100, 100)
+ b := gx.rgba(100, 100, 100, 100)
+ r := gx.rgba(200, 200, 200, 200)
+ assert (a + b).eq(r)
+}
+
+fn test_sub() {
+ a := gx.rgba(100, 100, 100, 100)
+ b := gx.rgba(100, 100, 100, 100)
+ r := gx.rgba(0, 0, 0, 0)
+ assert (a - b).eq(r)
+}
+
+fn test_mult() {
+ a := gx.rgba(10, 10, 10, 10)
+ b := gx.rgba(10, 10, 10, 10)
+ r := gx.rgba(100, 100, 100, 100)
+ assert (a * b).eq(r)
+}
+
+fn test_div() {
+ a := gx.rgba(100, 100, 100, 100)
+ b := gx.rgba(10, 10, 10, 10)
+ r := gx.rgba(10, 10, 10, 10)
+ assert (a / b).eq(r)
+}
+
+fn test_rgba8() {
+ assert gx.white.rgba8() == -1
+ assert gx.black.rgba8() == 255
+ assert gx.red.rgba8() == -16776961
+ assert gx.green.rgba8() == 16711935
+ assert gx.blue.rgba8() == 65535
+}
+
+fn test_bgra8() {
+ assert gx.white.bgra8() == -1
+ assert gx.black.bgra8() == 255
+ assert gx.red.bgra8() == 65535
+ assert gx.green.bgra8() == 16711935
+ assert gx.blue.bgra8() == -16776961
+}
+
+fn test_abgr8() {
+ assert gx.white.abgr8() == -1
+ assert gx.black.abgr8() == -16777216
+ assert gx.red.abgr8() == -16776961
+ assert gx.green.abgr8() == -16711936
+ assert gx.blue.abgr8() == -65536
+}
diff --git a/v_windows/v/vlib/gx/image.v b/v_windows/v/vlib/gx/image.v
new file mode 100644
index 0000000..4a20ea1
--- /dev/null
+++ b/v_windows/v/vlib/gx/image.v
@@ -0,0 +1,14 @@
+module gx
+
+pub struct Image {
+mut:
+ obj voidptr
+pub:
+ id int
+ width int
+ height int
+}
+
+pub fn (i Image) is_empty() bool {
+ return isnil(i.obj)
+}
diff --git a/v_windows/v/vlib/gx/text.c.v b/v_windows/v/vlib/gx/text.c.v
new file mode 100644
index 0000000..6bd3b8e
--- /dev/null
+++ b/v_windows/v/vlib/gx/text.c.v
@@ -0,0 +1,18 @@
+module gx
+
+import fontstash
+
+const used_import = fontstash.used_import
+
+pub enum HorizontalAlign {
+ left = C.FONS_ALIGN_LEFT
+ center = C.FONS_ALIGN_CENTER
+ right = C.FONS_ALIGN_RIGHT
+}
+
+pub enum VerticalAlign {
+ top = C.FONS_ALIGN_TOP
+ middle = C.FONS_ALIGN_MIDDLE
+ bottom = C.FONS_ALIGN_BOTTOM
+ baseline = C.FONS_ALIGN_BASELINE
+}
diff --git a/v_windows/v/vlib/gx/text.v b/v_windows/v/vlib/gx/text.v
new file mode 100644
index 0000000..fee934a
--- /dev/null
+++ b/v_windows/v/vlib/gx/text.v
@@ -0,0 +1,20 @@
+module gx
+
+// TODO: remove these and uae the enum everywhere
+pub const (
+ align_left = HorizontalAlign.left
+ align_right = HorizontalAlign.right
+)
+
+pub struct TextCfg {
+pub:
+ color Color = black
+ size int = 16
+ align HorizontalAlign = .left
+ vertical_align VerticalAlign = .top
+ max_width int
+ family string
+ bold bool
+ mono bool
+ italic bool
+}