CSS colors have 5 formats to process: - 6 digit hex code: "#ff23ee" --> [HTML]{FF23EE} - 3 digit hex code: "#f0e" --> [HTML]{FF00EE} - rgba: rgba(128, 255, 0, 0.5) --> [rgb]{0.502, 1.000, 0.000} - rgb: rgb(128, 255, 0,) --> [rbg]{0
(value, user_arg, command, comm_arg)
| 2485 | return None |
| 2486 | |
| 2487 | def color(value, user_arg, command, comm_arg): |
| 2488 | """ |
| 2489 | CSS colors have 5 formats to process: |
| 2490 | |
| 2491 | - 6 digit hex code: "#ff23ee" --> [HTML]{FF23EE} |
| 2492 | - 3 digit hex code: "#f0e" --> [HTML]{FF00EE} |
| 2493 | - rgba: rgba(128, 255, 0, 0.5) --> [rgb]{0.502, 1.000, 0.000} |
| 2494 | - rgb: rgb(128, 255, 0,) --> [rbg]{0.502, 1.000, 0.000} |
| 2495 | - string: red --> {red} |
| 2496 | |
| 2497 | Additionally rgb or rgba can be expressed in % which is also parsed. |
| 2498 | """ |
| 2499 | arg = user_arg if user_arg != "" else comm_arg |
| 2500 | |
| 2501 | if value[0] == "#" and len(value) == 7: # color is hex code |
| 2502 | return command, f"[HTML]{{{value[1:].upper()}}}{arg}" |
| 2503 | if value[0] == "#" and len(value) == 4: # color is short hex code |
| 2504 | val = f"{value[1].upper() * 2}{value[2].upper() * 2}{value[3].upper() * 2}" |
| 2505 | return command, f"[HTML]{{{val}}}{arg}" |
| 2506 | elif value[:3] == "rgb": # color is rgb or rgba |
| 2507 | r = re.findall("(?<=\\()[0-9\\s%]+(?=,)", value)[0].strip() |
| 2508 | r = float(r[:-1]) / 100 if "%" in r else int(r) / 255 |
| 2509 | g = re.findall("(?<=,)[0-9\\s%]+(?=,)", value)[0].strip() |
| 2510 | g = float(g[:-1]) / 100 if "%" in g else int(g) / 255 |
| 2511 | if value[3] == "a": # color is rgba |
| 2512 | b = re.findall("(?<=,)[0-9\\s%]+(?=,)", value)[1].strip() |
| 2513 | else: # color is rgb |
| 2514 | b = re.findall("(?<=,)[0-9\\s%]+(?=\\))", value)[0].strip() |
| 2515 | b = float(b[:-1]) / 100 if "%" in b else int(b) / 255 |
| 2516 | return command, f"[rgb]{{{r:.3f}, {g:.3f}, {b:.3f}}}{arg}" |
| 2517 | else: |
| 2518 | return command, f"{{{value}}}{arg}" # color is likely string-named |
| 2519 | |
| 2520 | CONVERTED_ATTRIBUTES: dict[str, Callable] = { |
| 2521 | "font-weight": font_weight, |