From 1361002adcf9c251531355de341f0e01f4a4283d Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Fri, 19 Sep 2025 19:33:54 +0800 Subject: [PATCH] gg: change the Color APIs, that `int` to use `i32` instead (#25349) --- vlib/builtin/js/string.js.v | 5 +++++ vlib/gg/color.v | 36 ++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/vlib/builtin/js/string.js.v b/vlib/builtin/js/string.js.v index ca414470f2..786a7d78ec 100644 --- a/vlib/builtin/js/string.js.v +++ b/vlib/builtin/js/string.js.v @@ -287,6 +287,11 @@ pub fn (s string) i16() i16 { return i16(JS.parseInt(s.str)) } +// i32 returns the value of the string as i32 `'1'.i32() == i32(1)`. +pub fn (s string) i32() i32 { + return i32(JS.parseInt(s.str)) +} + // f32 returns the value of the string as f32 `'1.0'.f32() == f32(1)`. pub fn (s string) f32() f32 { // return C.atof(&char(s.str)) diff --git a/vlib/gg/color.v b/vlib/gg/color.v index d239d3539c..81325725da 100644 --- a/vlib/gg/color.v +++ b/vlib/gg/color.v @@ -122,7 +122,7 @@ pub mut: } // hex takes in a 32 bit integer and splits it into 4 byte values -pub fn hex(color int) Color { +pub fn hex(color i32) Color { return Color{ r: u8((color >> 16) & 0xFF) g: u8((color >> 8) & 0xFF) @@ -151,10 +151,10 @@ pub fn rgba(r u8, g u8, b u8, a u8) Color { // + adds `b` to `a`, with a maximum value of 255 for each channel pub fn (a Color) + (b Color) Color { - mut na := int(a.a) + b.a - mut nr := int(a.r) + b.r - mut ng := int(a.g) + b.g - mut nb := int(a.b) + b.b + mut na := i32(a.a) + b.a + mut nr := i32(a.r) + b.r + mut ng := i32(a.g) + b.g + mut nb := i32(a.b) + b.b if na > 255 { na = 255 } @@ -178,9 +178,9 @@ pub fn (a Color) + (b Color) Color { // - subtracts `b` from `a`, with a minimum value of 0 for each channel pub fn (a Color) - (b Color) Color { mut na := if a.a > b.a { a.a } else { b.a } - mut nr := int(a.r) - b.r - mut ng := int(a.g) - b.g - mut nb := int(a.b) - b.b + mut nr := i32(a.r) - b.r + mut ng := i32(a.g) - b.g + mut nb := i32(a.b) - b.b if na < 0 { na = 0 } @@ -249,25 +249,25 @@ pub fn (c Color) str() string { return 'Color{${c.r}, ${c.g}, ${c.b}, ${c.a}}' } -// rgba8 converts a color value to an int in the RGBA8 order. +// rgba8 converts a color value to an 32bit int in the RGBA8 order. // see https://developer.apple.com/documentation/coreimage/ciformat @[inline] -pub fn (c Color) rgba8() int { - return int(u32(c.r) << 24 | u32(c.g) << 16 | u32(c.b) << 8 | u32(c.a)) +pub fn (c Color) rgba8() i32 { + return i32(u32(c.r) << 24 | u32(c.g) << 16 | u32(c.b) << 8 | u32(c.a)) } -// bgra8 converts a color value to an int in the BGRA8 order. +// bgra8 converts a color value to an 32bit int in the BGRA8 order. // see https://developer.apple.com/documentation/coreimage/ciformat @[inline] -pub fn (c Color) bgra8() int { - return int(u32(c.b) << 24 | u32(c.g) << 16 | u32(c.r) << 8 | u32(c.a)) +pub fn (c Color) bgra8() i32 { + return i32(u32(c.b) << 24 | u32(c.g) << 16 | u32(c.r) << 8 | u32(c.a)) } -// abgr8 converts a color value to an int in the ABGR8 order. +// abgr8 converts a color value to an 32bit int in the ABGR8 order. // see https://developer.apple.com/documentation/coreimage/ciformat @[inline] -pub fn (c Color) abgr8() int { - return int(u32(c.a) << 24 | u32(c.b) << 16 | u32(c.g) << 8 | u32(c.r)) +pub fn (c Color) abgr8() i32 { + return i32(u32(c.a) << 24 | u32(c.b) << 16 | u32(c.g) << 8 | u32(c.r)) } const string_colors = { @@ -298,7 +298,7 @@ const string_colors = { pub fn color_from_string(s string) Color { if s.starts_with('#') { mut hex_str := '0x' + s[1..] - return hex(hex_str.int()) + return hex(hex_str.i32()) } else { return string_colors[s] }