2020-12-06 00:24:08 +03:00
|
|
|
/**********************************************************************
|
|
|
|
* regex samples
|
|
|
|
*
|
2023-03-28 23:55:57 +03:00
|
|
|
* Copyright (c) 2019-2023 Dario Deledda. All rights reserved.
|
2020-12-06 00:24:08 +03:00
|
|
|
* Use of this source code is governed by an MIT license
|
|
|
|
* that can be found in the LICENSE file.
|
|
|
|
*
|
|
|
|
* This file contains a collection of regex samples
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
import regex
|
|
|
|
|
|
|
|
/*
|
2021-01-24 12:15:11 +03:00
|
|
|
This simple function converts an HTML RGB value with 3 or 6 hex digits to a u32 value,
|
|
|
|
this function is not optimized and it is only for didatical purpose
|
2020-12-06 00:24:08 +03:00
|
|
|
example: #A0B0CC #A9F
|
|
|
|
*/
|
|
|
|
fn convert_html_rgb(in_col string) u32 {
|
|
|
|
mut n_digit := if in_col.len == 4 { 1 } else { 2 }
|
|
|
|
mut col_mul := if in_col.len == 4 { 4 } else { 0 }
|
|
|
|
|
2021-01-24 12:15:11 +03:00
|
|
|
// this is the regex query, it uses V string interpolation to customize the regex query
|
2021-02-28 23:20:21 +03:00
|
|
|
// NOTE: if you want use escaped code you must use the r"" (raw) strings,
|
2021-01-24 12:15:11 +03:00
|
|
|
// *** please remember that V interpoaltion doesn't work on raw strings. ***
|
2020-12-06 00:24:08 +03:00
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
query := '#([a-fA-F0-9]{${n_digit}})([a-fA-F0-9]{${n_digit}})([a-fA-F0-9]{${n_digit}})'
|
2020-12-06 00:24:08 +03:00
|
|
|
|
2021-03-01 02:18:14 +03:00
|
|
|
mut re := regex.regex_opt(query) or { panic(err) }
|
2020-12-06 00:24:08 +03:00
|
|
|
start, end := re.match_string(in_col)
|
2022-11-15 16:53:13 +03:00
|
|
|
println('start: ${start}, end: ${end}')
|
2020-12-06 00:24:08 +03:00
|
|
|
mut res := u32(0)
|
|
|
|
if start >= 0 {
|
|
|
|
group_list := re.get_group_list()
|
2021-02-19 13:39:15 +03:00
|
|
|
r := ('0x' + in_col[group_list[0].start..group_list[0].end]).int() << col_mul
|
|
|
|
g := ('0x' + in_col[group_list[1].start..group_list[1].end]).int() << col_mul
|
|
|
|
b := ('0x' + in_col[group_list[2].start..group_list[2].end]).int() << col_mul
|
2022-11-15 16:53:13 +03:00
|
|
|
println('r: ${r} g: ${g} b: ${b}')
|
2020-12-06 00:24:08 +03:00
|
|
|
res = u32(r) << 16 | u32(g) << 8 | u32(b)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-01-24 12:15:11 +03:00
|
|
|
This function demonstrates the use of the named groups
|
2020-12-06 00:24:08 +03:00
|
|
|
*/
|
|
|
|
fn convert_html_rgb_n(in_col string) u32 {
|
|
|
|
mut n_digit := if in_col.len == 4 { 1 } else { 2 }
|
|
|
|
mut col_mul := if in_col.len == 4 { 4 } else { 0 }
|
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
query := '#(?P<red>[a-fA-F0-9]{${n_digit}})(?P<green>[a-fA-F0-9]{${n_digit}})(?P<blue>[a-fA-F0-9]{${n_digit}})'
|
2020-12-06 00:24:08 +03:00
|
|
|
|
2021-03-01 02:18:14 +03:00
|
|
|
mut re := regex.regex_opt(query) or { panic(err) }
|
2020-12-06 00:24:08 +03:00
|
|
|
start, end := re.match_string(in_col)
|
2022-11-15 16:53:13 +03:00
|
|
|
println('start: ${start}, end: ${end}')
|
2020-12-06 00:24:08 +03:00
|
|
|
mut res := u32(0)
|
|
|
|
if start >= 0 {
|
2021-02-19 13:39:15 +03:00
|
|
|
red_s, red_e := re.get_group_bounds_by_name('red')
|
|
|
|
r := ('0x' + in_col[red_s..red_e]).int() << col_mul
|
|
|
|
|
|
|
|
green_s, green_e := re.get_group_bounds_by_name('green')
|
|
|
|
g := ('0x' + in_col[green_s..green_e]).int() << col_mul
|
|
|
|
|
|
|
|
blue_s, blue_e := re.get_group_bounds_by_name('blue')
|
|
|
|
b := ('0x' + in_col[blue_s..blue_e]).int() << col_mul
|
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
println('r: ${r} g: ${g} b: ${b}')
|
2020-12-06 00:24:08 +03:00
|
|
|
res = u32(r) << 16 | u32(g) << 8 | u32(b)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-01-24 12:15:11 +03:00
|
|
|
// convert HTML rgb color using groups
|
2021-02-19 13:39:15 +03:00
|
|
|
println(convert_html_rgb('#A0b0Cc').hex())
|
|
|
|
println(convert_html_rgb('#ABC').hex())
|
2020-12-06 00:24:08 +03:00
|
|
|
|
2021-01-24 12:15:11 +03:00
|
|
|
// convert HTML rgb color using named groups
|
2021-02-19 13:39:15 +03:00
|
|
|
println(convert_html_rgb_n('#A0B0CC').hex())
|
|
|
|
println(convert_html_rgb_n('#ABC').hex())
|
2021-01-18 15:20:06 +03:00
|
|
|
}
|