From 6da1d3aff822a2508cfaa6a1b142adccf522e2d1 Mon Sep 17 00:00:00 2001 From: Ethan Reynolds Date: Thu, 28 May 2020 07:21:20 -0400 Subject: [PATCH] gx: convert Color values from int to byte and expand operators --- vlib/gx/color.v | 107 +++++++++++++++++++++++++++++++++++++++++++ vlib/gx/color_test.v | 47 +++++++++++++++++++ vlib/gx/gx.v | 99 --------------------------------------- vlib/gx/gx_test.v | 13 ------ vlib/gx/image.v | 14 ++++++ vlib/gx/text.v | 17 +++++++ 6 files changed, 185 insertions(+), 112 deletions(-) create mode 100644 vlib/gx/color.v create mode 100644 vlib/gx/color_test.v delete mode 100644 vlib/gx/gx.v delete mode 100644 vlib/gx/gx_test.v create mode 100644 vlib/gx/image.v create mode 100644 vlib/gx/text.v diff --git a/vlib/gx/color.v b/vlib/gx/color.v new file mode 100644 index 0000000000..d2c75f53dd --- /dev/null +++ b/vlib/gx/color.v @@ -0,0 +1,107 @@ +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, g, b byte) Color { + return Color{ + r: r, + g: g, + b: b + } +} + +pub fn rgba(r, g, b, 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}' +} diff --git a/vlib/gx/color_test.v b/vlib/gx/color_test.v new file mode 100644 index 0000000000..458bbbe6e6 --- /dev/null +++ b/vlib/gx/color_test.v @@ -0,0 +1,47 @@ +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) +} diff --git a/vlib/gx/gx.v b/vlib/gx/gx.v deleted file mode 100644 index 22c9142b61..0000000000 --- a/vlib/gx/gx.v +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. -// Use of this source code is governed by an MIT license -// that can be found in the LICENSE file. - -module gx - -pub struct Color { -pub: - r int - g int - b int -} - -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 } - - // Shades - 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 } -) - -pub const ( - align_left = 1 - align_right = 4 -) - -pub struct TextCfg { -pub: - color Color - size int - align int - max_width int - family string - bold bool - mono bool -} - -pub struct Image { -mut: - obj voidptr -pub: - id int - width int - height int -} - -pub fn (img Image) is_empty() bool { - return isnil(img.obj) -} - -pub fn (c Color) str() string { - return '{$c.r, $c.g, $c.b}' -} - -pub fn (a Color) eq(b Color) bool { - return a.r == b.r && a.g == b.g && a.b == b.b -} - -pub fn rgb(r, g, b int) Color { - res := Color { - r: r - g: g - b: b - } - return res -} - -pub fn hex(color int) Color { - res := Color { - r: (color >> 16) & 0xFF - g: (color >> 8) & 0xFF - b: color & 0xFF - } - return res -} - -// fn text_width_char(c char) int { -// return text_width(char2string(c)) -// // return C.text_width_char(c) -// } diff --git a/vlib/gx/gx_test.v b/vlib/gx/gx_test.v deleted file mode 100644 index 91c0cc7551..0000000000 --- a/vlib/gx/gx_test.v +++ /dev/null @@ -1,13 +0,0 @@ -import gx - -fn test_hex() { - // valid colors - color_a := gx.hex(0x6c5ce7) - color_b := gx.rgb(108, 92, 231) - assert color_a.eq(color_b) == true - - // doesn't give right value with short hex value - short_color := gx.hex(0xfff) - white_color := gx.rgb(255, 255, 255) - assert short_color.eq(white_color) == false -} diff --git a/vlib/gx/image.v b/vlib/gx/image.v new file mode 100644 index 0000000000..d5ef449860 --- /dev/null +++ b/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/vlib/gx/text.v b/vlib/gx/text.v new file mode 100644 index 0000000000..cfeba2a06c --- /dev/null +++ b/vlib/gx/text.v @@ -0,0 +1,17 @@ +module gx + +pub const ( + align_left = 1 + align_right = 4 +) + +pub struct TextCfg { +pub: + color Color + size int + align int + max_width int + family string + bold bool + mono bool +}