2
0
Fork 0

Made the textoutpc.color an independant module: thcolor.

This commit is contained in:
Thomas Touhey 2019-05-08 14:02:46 +02:00
parent 55946468fd
commit 0df1699c27
No known key found for this signature in database
GPG Key ID: 2ECEB0517AD947FB
8 changed files with 79 additions and 605 deletions

View File

@ -8,6 +8,7 @@ python_version = '3.7'
[packages]
regex = '*'
thcolor = { git = 'https://forge.touhey.org/thcolor.git' }
[dev-packages]
sphinx = '*'

103
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "befed1c16c1f4986f36ae7b114c67470fca119ca94a89bc557baef9c3a258eb6"
"sha256": "6505cb754ffede68e416ce5cf702dddf8f956a3181a4d58924aff57847b29ee9"
},
"pipfile-spec": 6,
"requires": {
@ -18,18 +18,22 @@
"default": {
"regex": {
"hashes": [
"sha256:0306149889c1a1bec362511f737bc446245ddfcdbe4b556abdfc506ed46dfa47",
"sha256:4b08704a5939c698d2d5950b5dc950597613216cc8c01048efc0da860a0c3db9",
"sha256:6ba0eb777ada6887062c2620e6d644b011078d5d3dc09119ae7107285f6f95e9",
"sha256:7789cc323948792c4c62b269a56f2f2f9bc77d44e54fd81e01b12a967dd7244c",
"sha256:825143aadca0da7d26eeaf2ab0f8bc33921a5642e570ded92dde08c5aaebc65f",
"sha256:8fbd057faab28ce552d89c46f7a968e950f07e80752dfb93891dd11c6b0ee3b4",
"sha256:a41aabb0b9072a14f1e2e554f959ed6439b83610ed656edace9096a0b27e378e",
"sha256:d8807231aed332a1d0456d2088967b87e8c664222bed8e566384ca0ec0b43bfd",
"sha256:dfd89b642fe71f4e8a9906455d4147d453061377b650e6233ddd9ea822971360"
"sha256:020429dcf9b76cc7648a99c81b3a70154e45afebc81e0b85364457fe83b525e4",
"sha256:0552802b1c3f3c7e4fee8c85e904a13c48226020aa1a0593246888a1ac55aaaf",
"sha256:308965a80b92e1fec263ac1e4f1094317809a72bc4d26be2ec8a5fd026301175",
"sha256:4d627feef04eb626397aa7bdec772774f53d63a1dc7cc5ee4d1bd2786a769d19",
"sha256:93d1f9fcb1d25e0b4bd622eeba95b080262e7f8f55e5b43c76b8a5677e67334c",
"sha256:c3859bbf29b1345d694f069ddfe53d6907b0393fda5e3794c800ad02902d78e9",
"sha256:d56ce4c7b1a189094b9bee3b81c4aeb3f1ba3e375e91627ec8561b6ab483d0a8",
"sha256:ebc5ef4e10fa3312fa1967dc0a894e6bd985a046768171f042ac3974fadc9680",
"sha256:f9cd39066048066a4abe4c18fb213bc541339728005e72263f023742fb912585"
],
"index": "pypi",
"version": "==2019.3.12"
"version": "==2019.4.14"
},
"thcolor": {
"git": "https://forge.touhey.org/thcolor.git",
"ref": "be32e9875ea503dd80bc00abaff12727fdb6c645"
}
},
"develop": {
@ -92,10 +96,10 @@
},
"jinja2": {
"hashes": [
"sha256:74c935a1b8bb9a3947c50a54766a969d4846290e1e788ea44c1392163723c3bd",
"sha256:f84be1bb0040caca4cea721fcbbbbd61f9be9464ca236387158b0feea01914a4"
"sha256:065c4f02ebe7f7cf559e49ee5a95fb800a9e4528727aec6f24402a5374c65013",
"sha256:14dd6caf1527abb21f08f86c784eac40853ba93edb79552aa1e4b8aef1b61c7b"
],
"version": "==2.10"
"version": "==2.10.1"
},
"markupsafe": {
"hashes": [
@ -139,10 +143,10 @@
},
"pudb": {
"hashes": [
"sha256:8d8b974641b7a7a2a721af01c9dce5eac8e05a2ceebc2680725ba8eef1ca876e"
"sha256:ac30cfc64580958ab7265decb4cabb9141f08781ff072e9a336d5a7942ce35a6"
],
"index": "pypi",
"version": "==2018.1"
"version": "==2019.1"
},
"pygments": {
"hashes": [
@ -153,17 +157,17 @@
},
"pyparsing": {
"hashes": [
"sha256:66c9268862641abcac4a96ba74506e594c884e3f57690a696d21ad8210ed667a",
"sha256:f6c5ef0d7480ad048c054c37632c67fca55299990fff127850181659eea33fc3"
"sha256:1873c03321fc118f4e9746baf201ff990ceb915f433f23b395f5580d1840cb2a",
"sha256:9b6323ef4ab914af344ba97510e966d64ba91055d6b9afa6b30799340e89cc03"
],
"version": "==2.3.1"
"version": "==2.4.0"
},
"pytz": {
"hashes": [
"sha256:32b0891edff07e28efe91284ed9c31e123d84bea3fd98e1f72be2508f43ef8d9",
"sha256:d5f05e487007e29e03409f9398d074e158d920d36eb82eaf66fb1136b0c5374c"
"sha256:303879e36b721603cc54604edcac9d20401bdbe31e1e4fdee5b9f98d5d31dfda",
"sha256:d747dd3d23d77ef44c6a3526e274af6efeb0a6f1afd5a69ba4d5be4098c8e141"
],
"version": "==2018.9"
"version": "==2019.1"
},
"requests": {
"hashes": [
@ -188,25 +192,60 @@
},
"sphinx": {
"hashes": [
"sha256:9f3e17c64b34afc653d7c5ec95766e03043cc6d80b0de224f59b6b6e19d37c3c",
"sha256:c7658aab75c920288a8cf6f09f244c6cfdae30d82d803ac1634d9f223a80ca08"
"sha256:423280646fb37944dd3c85c58fb92a20d745793a9f6c511f59da82fa97cd404b",
"sha256:de930f42600a4fef993587633984cc5027dedba2464bcf00ddace26b40f8d9ce"
],
"index": "pypi",
"version": "==1.8.5"
"version": "==2.0.1"
},
"sphinxcontrib-websupport": {
"sphinxcontrib-applehelp": {
"hashes": [
"sha256:68ca7ff70785cbe1e7bccc71a48b5b6d965d79ca50629606c7861a21b206d9dd",
"sha256:9de47f375baf1ea07cdb3436ff39d7a9c76042c10a769c52353ec46e4e8fc3b9"
"sha256:edaa0ab2b2bc74403149cb0209d6775c96de797dfd5b5e2a71981309efab3897",
"sha256:fb8dee85af95e5c30c91f10e7eb3c8967308518e0f7488a2828ef7bc191d0d5d"
],
"version": "==1.1.0"
"version": "==1.0.1"
},
"sphinxcontrib-devhelp": {
"hashes": [
"sha256:6c64b077937330a9128a4da74586e8c2130262f014689b4b89e2d08ee7294a34",
"sha256:9512ecb00a2b0821a146736b39f7aeb90759834b07e81e8cc23a9c70bacb9981"
],
"version": "==1.0.1"
},
"sphinxcontrib-htmlhelp": {
"hashes": [
"sha256:4670f99f8951bd78cd4ad2ab962f798f5618b17675c35c5ac3b2132a14ea8422",
"sha256:d4fd39a65a625c9df86d7fa8a2d9f3cd8299a3a4b15db63b50aac9e161d8eff7"
],
"version": "==1.0.2"
},
"sphinxcontrib-jsmath": {
"hashes": [
"sha256:2ec2eaebfb78f3f2078e73666b1415417a116cc848b72e5172e596c871103178",
"sha256:a9925e4a4587247ed2191a22df5f6970656cb8ca2bd6284309578f2153e0c4b8"
],
"version": "==1.0.1"
},
"sphinxcontrib-qthelp": {
"hashes": [
"sha256:513049b93031beb1f57d4daea74068a4feb77aa5630f856fcff2e50de14e9a20",
"sha256:79465ce11ae5694ff165becda529a600c754f4bc459778778c7017374d4d406f"
],
"version": "==1.0.2"
},
"sphinxcontrib-serializinghtml": {
"hashes": [
"sha256:c0efb33f8052c04fd7a26c0a07f1678e8512e0faec19f4aa8f2473a8b81d5227",
"sha256:db6615af393650bf1151a6cd39120c29abaf93cc60db8c48eb2dddbfdc3a9768"
],
"version": "==1.1.3"
},
"urllib3": {
"hashes": [
"sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39",
"sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22"
"sha256:2393a695cd12afedd0dcb26fe5d50d0cf248e5a66f75dbd89a3d4eb333a61af4",
"sha256:a637e5fae88995b256e3409dc4d52c2e2e0ba32c42a6365fee8bbd2238de3cfb"
],
"version": "==1.24.1"
"version": "==1.24.3"
},
"urwid": {
"hashes": [

View File

@ -1,7 +1,7 @@
[metadata]
name = textoutpc
version = attr: textoutpc.version
url = https://textout.touhey.fr/
url = https://textout.touhey.pro/
author = Thomas Touhey
author_email = thomas@touhey.fr
description = textout() equivalent from Planète Casio
@ -21,13 +21,14 @@ classifiers =
[options]
zip_safe = False
include_package_data = True
packages = textoutpc, textoutpc.color, textoutpc.builtin
packages = textoutpc, textoutpc.builtin
test_suite = test
scripts =
scripts/textout2html
scripts/textout2ls
install_requires =
regex
thcolor
[options.package_data]
* = *.txt, *.rst

View File

@ -5,10 +5,13 @@
#******************************************************************************
from .. import InlineTag as _InlineTag
from ..color import get_color as _get_color
from thcolor import Color as _Color
__all__ = ["TextTag"]
def _get_color(text):
return _Color.from_text(text).rgba()
# ---
# Data.
# ---

View File

@ -1,15 +0,0 @@
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
""" HTML/CSS-like color parsing, mainly for the `[color]` tag.
Defines the `get_color()` function which returns an rgba value.
The functions in this module do not aim at being totally compliant with
the W3C standards, although it is inspired from it.
"""
from ._read import get_color
# End of file.

View File

@ -1,168 +0,0 @@
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
""" Named colors definitions. Color names are case-insensitive.
Taken from: https://www.w3schools.com/cssref/css_colors.asp """
__all__ = ["colors"]
colors = {
# Standard CSS3 named colors, ordered by hex code.
'black': '#000000',
'navy': '#000080',
'darkblue': '#00008B',
'mediumblue': '#0000CD',
'blue': '#0000FF',
'darkgreen': '#006400',
'green': '#008000',
'teal': '#008080',
'darkcyan': '#008B8B',
'deepskyblue': '#00BFFF',
'darkturquoise': '#00CED1',
'mediumspringgreen': '#00FA9A',
'lime': '#00FF00',
'springgreen': '#00FF7F',
'aqua': '#00FFFF',
'cyan': '#00FFFF',
'midnightblue': '#191970',
'dodgerblue': '#1E90FF',
'lightseagreen': '#20B2AA',
'forestgreen': '#228B22',
'seagreen': '#2E8B57',
'darkslategray': '#2F4F4F',
'darkslategrey': '#2F4F4F',
'limegreen': '#32CD32',
'mediumseagreen': '#3CB371',
'turquoise': '#40E0D0',
'royalblue': '#4169E1',
'steelblue': '#4682B4',
'darkslateblue': '#483D8B',
'mediumturquoise': '#48D1CC',
'indigo': '#4B0082',
'darkolivegreen': '#556B2F',
'cadetblue': '#5F9EA0',
'cornflowerblue': '#6495ED',
'rebeccapurple': '#663399',
'mediumaquamarine': '#66CDAA',
'dimgray': '#696969',
'dimgrey': '#696969',
'slateblue': '#6A5ACD',
'olivedrab': '#6B8E23',
'slategray': '#708090',
'slategrey': '#708090',
'lightslategray': '#778899',
'lightslategrey': '#778899',
'mediumslateblue': '#7B68EE',
'lawngreen': '#7CFC00',
'chartreuse': '#7FFF00',
'aquamarine': '#7FFFD4',
'maroon': '#800000',
'purple': '#800080',
'olive': '#808000',
'gray': '#808080',
'grey': '#808080',
'skyblue': '#87CEEB',
'lightskyblue': '#87CEFA',
'blueviolet': '#8A2BE2',
'darkred': '#8B0000',
'darkmagenta': '#8B008B',
'saddlebrown': '#8B4513',
'darkseagreen': '#8FBC8F',
'lightgreen': '#90EE90',
'mediumpurple': '#9370DB',
'darkviolet': '#9400D3',
'palegreen': '#98FB98',
'darkorchid': '#9932CC',
'yellowgreen': '#9ACD32',
'sienna': '#A0522D',
'brown': '#A52A2A',
'darkgray': '#A9A9A9',
'darkgrey': '#A9A9A9',
'lightblue': '#ADD8E6',
'greenyellow': '#ADFF2F',
'paleturquoise': '#AFEEEE',
'lightsteelblue': '#B0C4DE',
'powderblue': '#B0E0E6',
'firebrick': '#B22222',
'darkgoldenrod': '#B8860B',
'mediumorchid': '#BA55D3',
'rosybrown': '#BC8F8F',
'darkkhaki': '#BDB76B',
'silver': '#C0C0C0',
'mediumvioletred': '#C71585',
'indianred': '#CD5C5C',
'peru': '#CD853F',
'chocolate': '#D2691E',
'tan': '#D2B48C',
'lightgray': '#D3D3D3',
'lightgrey': '#D3D3D3',
'thistle': '#D8BFD8',
'orchid': '#DA70D6',
'goldenrod': '#DAA520',
'palevioletred': '#DB7093',
'crimson': '#DC143C',
'gainsboro': '#DCDCDC',
'plum': '#DDA0DD',
'burlywood': '#DEB887',
'lightcyan': '#E0FFFF',
'lavender': '#E6E6FA',
'darksalmon': '#E9967A',
'violet': '#EE82EE',
'palegoldenrod': '#EEE8AA',
'lightcoral': '#F08080',
'khaki': '#F0E68C',
'aliceblue': '#F0F8FF',
'honeydew': '#F0FFF0',
'azure': '#F0FFFF',
'sandybrown': '#F4A460',
'wheat': '#F5DEB3',
'beige': '#F5F5DC',
'whitesmoke': '#F5F5F5',
'mintcream': '#F5FFFA',
'ghostwhite': '#F8F8FF',
'salmon': '#FA8072',
'antiquewhite': '#FAEBD7',
'linen': '#FAF0E6',
'lightgoldenrodyellow': '#FAFAD2',
'oldlace': '#FDF5E6',
'red': '#FF0000',
'magenta': '#FF00FF',
'fuchsia': '#FF00FF',
'deeppink': '#FF1493',
'orangered': '#FF4500',
'tomato': '#FF6347',
'hotpink': '#FF69B4',
'coral': '#FF7F50',
'darkorange': '#FF8C00',
'lightsalmon': '#FFA07A',
'orange': '#FFA500',
'lightpink': '#FFB6C1',
'pink': '#FFC0CB',
'gold': '#FFD700',
'peachpuff': '#FFDAB9',
'navajowhite': '#FFDEAD',
'moccasin': '#FFE4B5',
'bisque': '#FFE4C4',
'mistyrose': '#FFE4E1',
'blanchedalmond': '#FFEBCD',
'papayawhip': '#FFEFD5',
'lavenderblush': '#FFF0F5',
'seashell': '#FFF5EE',
'cornsilk': '#FFF8DC',
'lemonchiffon': '#FFFACD',
'floralwhite': '#FFFAF0',
'snow': '#FFFAFA',
'yellow': '#FFFF00',
'lightyellow': '#FFFFE0',
'ivory': '#FFFFF0',
'white': '#FFFFFF',
# Keyword/special named color.
'transparent': 'rgba(0,0,0,0)',
}
# End of file.

View File

@ -1,365 +0,0 @@
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
""" HTML/CSS like color parsing, mainly for the `[color]` tag.
Defines the `get_color()` function which returns an rgba value.
"""
import re as _re
import math as _math
from ._named import colors as _named_colors
from ._sys import hls_to_rgb as _hls_to_rgb, hwb_to_rgb as _hwb_to_rgb
__all__ = ["get_color"]
_cr = _re.compile("""
rgba?\s*\(
\s* ((?P<rgb_r>[0-9]{1,3})
|(?P<rgb_r_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<rgb_g>[0-9]{1,3})
|(?P<rgb_g_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<rgb_b>[0-9]{1,3})
|(?P<rgb_b_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s/]
\s* ((?P<rgb_a_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<rgb_a_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*
)?)?)?
\)|
rbga?\s*\(
\s* ((?P<rbg_r>[0-9]{1,3})
|(?P<rbg_r_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<rbg_b>[0-9]{1,3})
|(?P<rbg_b_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<rbg_g>[0-9]{1,3})
|(?P<rbg_g_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s/]
\s* ((?P<rbg_a_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<rbg_a_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*
)?)?)?
\)|
brga?\s*\(
\s* ((?P<brg_b>[0-9]{1,3})
|(?P<brg_b_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<brg_r>[0-9]{1,3})
|(?P<brg_r_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<brg_g>[0-9]{1,3})
|(?P<brg_g_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s/]
\s* ((?P<brg_a_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<brg_a_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*
)?)?)?
\)|
bgra?\s*\(
\s* ((?P<bgr_b>[0-9]{1,3})
|(?P<bgr_b_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<bgr_g>[0-9]{1,3})
|(?P<bgr_g_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<bgr_r>[0-9]{1,3})
|(?P<bgr_r_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s/]
\s* ((?P<bgr_a_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<bgr_a_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*
)?)?)?
\)|
grba?\s*\(
\s* ((?P<grb_g>[0-9]{1,3})
|(?P<grb_g_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<grb_r>[0-9]{1,3})
|(?P<grb_r_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<grb_b>[0-9]{1,3})
|(?P<grb_b_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s/]
\s* ((?P<grb_a_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<grb_a_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*
)?)?)?
\)|
gbra?\s*\(
\s* ((?P<gbr_g>[0-9]{1,3})
|(?P<gbr_g_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<gbr_b>[0-9]{1,3})
|(?P<gbr_b_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s]
\s* ((?P<gbr_r>[0-9]{1,3})
|(?P<gbr_r_per> ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*%) \s* ([,\\s/]
\s* ((?P<gbr_a_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<gbr_a_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*
)?)?)?
\)|
lab\s*\(
\s* (?P<lab_l>-?[0-9]{1,3}) \s* [,\\s]
\s* (?P<lab_a>-?[0-9]{1,3}) \s* [,\\s]
\s* (?P<lab_b>-?[0-9]{1,3}) \s* ([,\\s/]
\s* ((?P<lab_a_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<lab_a_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*
)?
\)|
lch\s*\(
\s* (?P<lch_l>-?[0-9]{1,3}) \s* [,\\s]
\s* (?P<lch_ch>-?[0-9]{1,3}) \s* [,\\s]
\s* (?P<lch_hue>-? ([0-9]+\.?|[0-9]*\.[0-9]+) )
(?P<lch_agl>deg|grad|rad|turn|) \s* ([,\\s/]
\s* ((?P<lch_a_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<lch_a_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*
)?
\)|
gray\s*\(
\s* (?P<gray_l>-?[0-9]{1,3}) \s* ([,\\s/]
\s* ((?P<gray_a_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<gray_a_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*
)?
\)|
hsla?\s*\(
\s* (?P<hsl_hue>-? ([0-9]+\.?|[0-9]*\.[0-9]+) )
(?P<hsl_agl>deg|grad|rad|turn|) \s*[,\\s]
\s* ((?P<hsl_sat_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<hsl_sat_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*[,\\s]
\s* ((?P<hsl_lgt_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<hsl_lgt_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*([,\\s/]
\s* ((?P<hsl_aph_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<hsl_aph_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*
)?
\)|
hlsa?\s*\(
\s* (?P<hls_hue>-? ([0-9]+\.?|[0-9]*\.[0-9]+) )
(?P<hls_agl>deg|grad|rad|turn|) \s*[,\\s]
\s* ((?P<hls_lgt_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<hls_lgt_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*[,\\s]
\s* ((?P<hls_sat_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<hls_sat_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*([,\\s/]
\s* ((?P<hls_aph_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<hls_aph_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*)?
\)|
hwb\s*\(
\s* (?P<hwb_hue>-? ([0-9]+\.?|[0-9]*\.[0-9]+) )
(?P<hwb_agl>deg|grad|rad|turn|) \s*[,\\s]
\s* ((?P<hwb_wht_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<hwb_wht_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*[,\\s]
\s* ((?P<hwb_blk_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<hwb_blk_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*([,\\s/]
\s* ((?P<hwb_aph_per> ([0-9]+\.?|[0-9]*\.[0-9]+) ) \s*%
|(?P<hwb_aph_flt> (0*[01]\.?|0*\.[0-9]+) )) \s*)?
\)|
\# (?P<hex_digits> [0-9a-f]+)
|
(?P<legacy_chars> [0-9a-z]+)
""", _re.VERBOSE | _re.I | _re.M)
_rgb = ('rgb', 'rbg', 'brg', 'bgr', 'grb', 'gbr')
def get_color(value):
""" Get a color from a string.
Returns an (r, g, b, a) color.
Raises an exception if the color could not be read. """
# Check if is a color name.
value = value.strip()
try: value = _named_colors[value.lower()]
except: pass
# Initialize the alpha.
alpha = 1.0
# Get the match.
match = _cr.fullmatch(value)
if not match:
raise ValueError("invalid color string")
match = match.groupdict()
if match['hex_digits'] or match['legacy_chars']:
# Imitate the Netscape behaviour. Find more about this here:
# https://stackoverflow.com/a/8333464
#
# I've also extended the thing as I could to introduce more
# modern syntaxes described on the dedicated MDN page:
# https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
#
# First of all, depending on our source, we will act differently:
# - if we are using the `hex_digits` source, then we use the modern
# behaviour and do the fancy things such as `#ABC -> #AABBCC`
# management and possible alpha decoding;
# - if we are using the `legacy_chars` source, then we sanitize our
# input by replacing invalid characters by '0' characters (the
# 0xFFFF limit is due to how UTF-16 was managed at the time).
# We shall also truncate our input to 128 characters.
#
# After these sanitization options, we will keep the same method as
# for legacy color decoding. It should work and be tolerant enough…
members = 3
if match['hex_digits']:
hx = match['hex_digits'].lower()
# RGB and RGBA (3 and 4 char.) notations.
if len(hx) in (3, 4):
hx = hx[0:1] * 2 + hx[1:2] * 2 + hx[2:3] * 2 + hx[3:4] * 2
# Check if there is transparency or not.
if len(hx) % 3 != 0 and len(hx) % 4 == 0:
members = 4
else: # our source is `legacy_chars`
hx = match['legacy_chars'].lower()
hx = ''.join(c if c in '0123456789abcdef' \
else ('0', '00')[ord(c) > 0xFFFF] for c in hx[:128])[:128]
# First, calculate some values we're going to need.
# `iv` is the size of the zone for a member.
# `sz` is the size of the digits slice to take in that zone (max. 8).
# `of` is the offset in the zone of the slice to take.
iv = _math.ceil(len(hx) / members)
of = iv - 8 if iv > 8 else 0
sz = iv - of
# Then isolate the slices using the values calculated above.
# `gr` will be an array of 3 or 4 digit strings (depending on the
# number of members).
gr = list(map(lambda i: hx[i * iv + of:i * iv + iv] \
.ljust(sz, '0'), range(members)))
# Check how many digits we can skip at the beginning of each slice.
pre = min(map(lambda x: len(x) - len(x.lstrip('0')), gr))
pre = min(pre, sz - 2)
# Then extract the values.
it = map(lambda x: int('0' + x[pre:pre + 2], 16), gr)
if members == 3:
r, g, b = it
else:
r, g, b, alpha = it
alpha /= 255.0
elif any(match[key + '_r'] or match[key + '_r_per'] for key in _rgb):
# Extract the values.
for key in _rgb:
if not match[key + '_r'] and not match[key + '_r_per']:
continue
r = match[f'{key}_r']
rp = match[f'{key}_r_per']
g = match[f'{key}_g']
gp = match[f'{key}_g_per']
b = match[f'{key}_b']
bp = match[f'{key}_b_per']
ap = match[f'{key}_a_per']
af = match[f'{key}_a_flt']
break
r = int(r) if r else int(int(rp) * 255 / 100)
g = int(g) if g else int(int(gp) * 255 / 100) if gp else 0
b = int(b) if b else int(int(bp) * 255 / 100) if bp else 0
if ap:
alpha = float(ap) / 100.0
elif af:
alpha = float(af)
elif match['hsl_hue'] or match['hls_hue']:
# Extract the values.
if match['hsl_hue']:
hue = float(match['hsl_hue'])
agl = match['hsl_agl']
# Saturation.
if match['hsl_sat_per']:
sat = float(match['hsl_sat_per']) / 100.0
else:
sat = float(match['hsl_sat_flt'])
if sat > 1.0:
sat /= 100.0
# Light.
if match['hsl_lgt_per']:
lgt = float(match['hsl_lgt_per']) / 100.0
else:
lgt = float(match['hsl_lgt_flt'])
if lgt > 1.0:
lgt /= 100.0
# Alpha value.
if match['hsl_aph_per']:
alpha = float(match['hsl_aph_per']) / 100.0
elif match['hsl_aph_flt']:
alpha = float(match['hsl_aph_flt'])
else:
hue = float(match['hls_hue'])
agl = match['hls_agl']
# Saturation.
if match['hls_sat_per']:
sat = float(match['hls_sat_per']) / 100.0
else:
sat = float(match['hls_sat_flt'])
# Light.
if match['hls_lgt_per']:
lgt = float(match['hls_lgt_per']) / 100.0
else:
lgt = float(match['hls_lgt_flt'])
# Alpha value.
if match['hls_aph_per']:
alpha = float(match['hls_aph_per']) / 100.0
elif match['hls_aph_flt']:
alpha = float(match['hls_aph_flt'])
# Prepare the angle.
if agl == 'grad':
hue = hue * 400.0
elif agl == 'rad':
hue = hue / (2 * _math.pi)
elif not agl or agl == 'deg':
hue = hue / 360.0
hue = hue % 1.0
if sat > 1 or lgt > 1:
raise Exception
r, g, b = _hls_to_rgb(hue, lgt, sat)
r, g, b = map(lambda x:int(round(x * 255)), (r, g, b))
elif match['hwb_hue']:
hue = float(match['hwb_hue'])
agl = match['hwb_agl']
# Prepare the angle.
if agl == 'grad':
hue = hue * 400.0
elif agl == 'rad':
hue = hue / (2 * _math.pi)
elif not agl or agl == 'deg':
hue = hue / 360.0
hue = hue % 1.0
# Saturation.
if match['hwb_wht_per']:
wht = float(match['hwb_wht_per']) / 100.0
else:
wht = float(match['hwb_wht_flt'])
# Light.
if match['hwb_blk_per']:
blk = float(match['hwb_blk_per']) / 100.0
else:
blk = float(match['hwb_blk_flt'])
if wht > 1 or blk > 1:
raise Exception
r, g, b = _hwb_to_rgb(hue, wht, blk)
r, g, b = map(lambda x: int(round(x * 255)), (r, g, b))
else:
raise ValueError("unsupported format yet")
if r < 0 or r > 255 or g < 0 or g > 255 or b < 0 or b > 255:
raise ValueError("invalid color string")
if alpha < 0.0 or alpha > 1.0:
raise ValueError("invalid color string")
alpha = round(alpha, 4)
return (r, g, b, alpha)
# End of file.

View File

@ -1,22 +0,0 @@
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
""" Conversions between color systems. """
from colorsys import hls_to_rgb
__all__ = ["hls_to_rgb", "hwb_to_rgb"]
def hwb_to_rgb(hue, w, b):
""" Convert HWB to RGB color.
https://drafts.csswg.org/css-color/#hwb-to-rgb """
r, g, b = hls_to_rgb(hue, 0.5, 1.0)
f = lambda x: x * (1 - w - b) + w
r, g, b = f(r), f(g), f(b)
return r, g, b
# End of file.