From f5a8d883d2d2e18c0c804b03a1e0f73277a1d993 Mon Sep 17 00:00:00 2001 From: Nguyen Viet Hung Date: Sat, 7 Mar 2020 16:30:35 +1300 Subject: [PATCH] gx: add hex to rgb color --- vlib/gx/gx.v | 9 +++++++++ vlib/gx/gx_test.v | 13 +++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 vlib/gx/gx_test.v diff --git a/vlib/gx/gx.v b/vlib/gx/gx.v index aae1100fdc..a28532d902 100644 --- a/vlib/gx/gx.v +++ b/vlib/gx/gx.v @@ -93,6 +93,15 @@ pub fn rgb(r, g, b int) Color { 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 new file mode 100644 index 0000000000..91c0cc7551 --- /dev/null +++ b/vlib/gx/gx_test.v @@ -0,0 +1,13 @@ +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 +}