This commit is contained in:
blueloveTH 2024-08-06 12:58:11 +08:00
parent 0bd25e7224
commit f9a1bd1d49
5 changed files with 0 additions and 245 deletions

View File

@ -14,7 +14,6 @@ extern const char kPythonLibs_bisect[];
extern const char kPythonLibs_builtins[];
extern const char kPythonLibs_cmath[];
extern const char kPythonLibs_collections[];
extern const char kPythonLibs_colorsys[];
extern const char kPythonLibs_datetime[];
extern const char kPythonLibs_functools[];
extern const char kPythonLibs_heapq[];

View File

@ -1,171 +0,0 @@
"""Conversion functions between RGB and other color systems.
This modules provides two functions for each color system ABC:
rgb_to_abc(r, g, b) --> a, b, c
abc_to_rgb(a, b, c) --> r, g, b
All inputs and outputs are triples of floats in the range [0.0...1.0]
(with the exception of I and Q, which covers a slightly larger range).
Inputs outside the valid range may cause exceptions or invalid outputs.
Supported color systems:
RGB: Red, Green, Blue components
YIQ: Luminance, Chrominance (used by composite video signals)
HLS: Hue, Luminance, Saturation
HSV: Hue, Saturation, Value
"""
# References:
# http://en.wikipedia.org/wiki/YIQ
# http://en.wikipedia.org/wiki/HLS_color_space
# http://en.wikipedia.org/wiki/HSV_color_space
__all__ = ["rgb_to_yiq","yiq_to_rgb","rgb_to_hls","hls_to_rgb",
"rgb_to_hsv","hsv_to_rgb"]
# Some floating point constants
ONE_THIRD = 1.0/3.0
ONE_SIXTH = 1.0/6.0
TWO_THIRD = 2.0/3.0
# YIQ: used by composite video signals (linear combinations of RGB)
# Y: perceived grey level (0.0 == black, 1.0 == white)
# I, Q: color components
#
# There are a great many versions of the constants used in these formulae.
# The ones in this library uses constants from the FCC version of NTSC.
def rgb_to_yiq(r, g, b):
y = 0.30*r + 0.59*g + 0.11*b
i = 0.74*(r-y) - 0.27*(b-y)
q = 0.48*(r-y) + 0.41*(b-y)
return (y, i, q)
def yiq_to_rgb(y, i, q):
# r = y + (0.27*q + 0.41*i) / (0.74*0.41 + 0.27*0.48)
# b = y + (0.74*q - 0.48*i) / (0.74*0.41 + 0.27*0.48)
# g = y - (0.30*(r-y) + 0.11*(b-y)) / 0.59
r = y + 0.9468822170900693*i + 0.6235565819861433*q
g = y - 0.27478764629897834*i - 0.6356910791873801*q
b = y - 1.1085450346420322*i + 1.7090069284064666*q
if r < 0.0:
r = 0.0
if g < 0.0:
g = 0.0
if b < 0.0:
b = 0.0
if r > 1.0:
r = 1.0
if g > 1.0:
g = 1.0
if b > 1.0:
b = 1.0
return (r, g, b)
# HLS: Hue, Luminance, Saturation
# H: position in the spectrum
# L: color lightness
# S: color saturation
def rgb_to_hls(r, g, b):
maxc = max(r, g, b)
minc = min(r, g, b)
sumc = (maxc+minc)
rangec = (maxc-minc)
l = sumc/2.0
if minc == maxc:
return 0.0, l, 0.0
if l <= 0.5:
s = rangec / sumc
else:
s = rangec / (2.0-maxc-minc) # Not always 2.0-sumc: gh-106498.
rc = (maxc-r) / rangec
gc = (maxc-g) / rangec
bc = (maxc-b) / rangec
if r == maxc:
h = bc-gc
elif g == maxc:
h = 2.0+rc-bc
else:
h = 4.0+gc-rc
# h = (h/6.0) % 1.0
h = h / 6.0
h = h - int(h)
return h, l, s
def hls_to_rgb(h, l, s):
if s == 0.0:
return l, l, l
if l <= 0.5:
m2 = l * (1.0+s)
else:
m2 = l+s-(l*s)
m1 = 2.0*l - m2
return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
def _v(m1, m2, hue):
# hue = hue % 1.0
hue = hue - int(hue)
if hue < ONE_SIXTH:
return m1 + (m2-m1)*hue*6.0
if hue < 0.5:
return m2
if hue < TWO_THIRD:
return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
return m1
# HSV: Hue, Saturation, Value
# H: position in the spectrum
# S: color saturation ("purity")
# V: color brightness
def rgb_to_hsv(r, g, b):
maxc = max(r, g, b)
minc = min(r, g, b)
rangec = (maxc-minc)
v = maxc
if minc == maxc:
return 0.0, 0.0, v
s = rangec / maxc
rc = (maxc-r) / rangec
gc = (maxc-g) / rangec
bc = (maxc-b) / rangec
if r == maxc:
h = bc-gc
elif g == maxc:
h = 2.0+rc-bc
else:
h = 4.0+gc-rc
# h = (h/6.0) % 1.0
h = h / 6.0
h = h - int(h)
return h, s, v
def hsv_to_rgb(h, s, v):
if s == 0.0:
return v, v, v
i = int(h*6.0) # XXX assume int() truncates!
f = (h*6.0) - i
p = v*(1.0 - s)
q = v*(1.0 - s*f)
t = v*(1.0 - s*(1.0-f))
i = i%6
if i == 0:
return v, t, p
if i == 1:
return q, v, p
if i == 2:
return p, v, t
if i == 3:
return p, q, v
if i == 4:
return t, p, v
if i == 5:
return v, p, q
# Cannot get here

File diff suppressed because one or more lines are too long

View File

@ -1,21 +0,0 @@
a = '测试 123'
a = a.encode()
import base64
b = base64.b64encode(a)
c = base64.b64decode(b)
assert a == c
data = [66, 110, 145, 18, 176, 13, 255, 202, 173, 109, 178, 194, 171, 198, 143, 24, 113, 46, 70, 94, 71, 140, 159, 191, 134, 230, 190, 224, 223, 94, 217, 20, 241, 138, 104, 120, 249, 91, 134, 48, 108, 49, 0, 249, 235, 225, 228, 190, 63, 204, 216, 102, 153, 51, 79, 221, 234, 252, 231, 156, 74, 23, 131, 161, 172, 157, 26, 15, 88, 28, 21, 170, 86, 177, 177, 249, 111, 230, 35, 180, 61, 140, 33, 14, 74, 238, 253, 19, 177, 76, 249, 21, 35, 105, 24, 136, 187, 121, 71, 202, 239, 235, 71, 126, 60, 37, 83, 186, 102, 114, 95, 212, 81, 48, 102, 167, 208, 66, 250, 132, 199, 137, 141, 231, 126, 219, 125, 1, 86, 87, 132, 161, 55, 166, 192, 27, 95, 27, 237, 225, 32, 240, 234, 160, 247, 143, 241, 232, 195, 117, 83, 133, 69, 178, 239, 123, 144, 172, 34, 43, 56, 136, 184, 68, 65, 70, 61, 164, 109, 134, 142, 153, 125, 154, 62, 117, 166, 86, 234, 39, 73, 207, 67, 91, 88, 220, 43, 148, 201, 185, 128, 93, 151, 210, 167, 82, 87, 246, 171, 125, 210, 46, 60, 156, 4, 173, 219, 149, 24, 226, 63, 176, 92, 103, 126, 201, 254, 6, 186, 233, 165, 169, 237, 141, 252, 0, 195, 212, 222, 186, 103, 15, 137, 41, 251, 16, 163, 22, 177, 232, 205, 58, 50, 205, 89, 249, 38, 45, 98, 42, 155, 33, 225, 232, 16, 157, 91, 246, 207, 164, 150, 214, 76, 151, 179, 203, 67, 194, 213, 83, 2, 106, 109, 254, 15, 110, 168, 19, 114, 185, 174, 20, 106, 141, 116, 222, 205, 135, 222, 110, 90, 27, 61, 6, 118, 50, 155, 6, 224, 213, 109, 98, 252, 84, 166, 77, 124, 187, 187, 113, 173, 45, 17, 232, 208, 126, 248, 239, 18, 33, 205, 117, 44, 32, 223, 1, 221, 210, 41, 67, 28, 218, 218, 161, 209, 11, 93, 250, 96, 2, 43, 157, 217, 134, 183, 24, 105, 177, 74, 214, 18, 114, 191, 64, 195, 94, 194, 19, 115, 211, 103, 49, 218, 87, 8, 199, 50, 225, 174, 222, 75, 23, 159, 76, 56, 208, 224, 172, 48, 197, 126, 159, 191, 80, 216, 148, 30, 114, 231, 142, 100, 159, 67, 77, 190, 64, 182, 21, 108, 4, 232, 73, 145, 247, 196, 220, 197, 234, 55, 241, 212, 115, 115, 142, 172, 248, 132, 117, 115, 107, 176, 230, 130, 189, 160, 150, 63, 79, 253, 240, 113, 61, 222, 46, 102, 118, 100, 208, 170, 0, 60, 154, 102, 168, 241, 159, 146, 71, 55, 244, 123, 82, 49, 64, 231, 190, 49, 51, 16, 111, 153, 209, 208, 116, 19, 68, 139, 208, 105, 248, 80, 12, 237, 29, 63, 80, 127, 1, 118, 22, 39, 83, 25, 220, 75, 31, 152, 16, 94, 254, 141, 55, 6, 89, 45, 247, 229, 209, 239, 223, 226, 124, 50, 51, 219, 110, 100, 251, 122, 53, 166, 63, 43, 116, 190, 114, 169, 72, 18, 190, 55, 4, 249, 3, 200, 99, 0, 37, 94, 50, 58, 37, 56, 154, 18, 154, 127, 123, 187, 123, 110, 131, 14, 185, 76, 193, 11, 227, 36, 184, 88, 3, 222, 126, 32, 143, 125, 180, 104, 142, 84, 22, 53, 2, 38, 188, 187, 51, 163, 189, 25, 215, 94, 190, 196, 213, 155, 23, 84, 206, 237, 125, 76, 185, 12, 111, 201, 249, 101, 50, 217, 32, 3, 37, 49, 177, 4, 10, 123, 29, 126, 106, 108, 246, 89, 42, 182, 135, 11, 152, 122, 12, 23, 159, 212, 53, 44, 244, 48, 251, 130, 109, 191, 76, 148, 226, 83, 55, 225, 100, 196, 166, 171, 108, 91, 67, 226, 207, 143, 73, 81, 95, 69, 92, 141, 150, 108, 168, 235, 1, 33, 160, 158, 62, 149, 0, 200, 228, 176, 38, 112, 18, 253, 239, 107, 214, 17, 22, 112, 255, 117, 155, 248, 59, 113, 100, 145, 101, 245, 113, 230, 167, 58, 232, 195, 51, 76, 26, 7, 94, 201, 198, 96, 93, 8, 231, 60, 139, 37, 191, 37, 101, 155, 83, 246, 181, 109, 149, 241, 96, 168, 126, 232, 54, 230, 197, 179, 214, 148, 79, 13, 27, 195, 164, 146, 183, 129, 82, 82, 177, 2, 255, 8, 85, 214, 83, 244, 237, 143, 104, 107, 28, 215, 178, 46, 71, 175, 186, 77, 191, 93, 13, 204, 154, 234, 193, 231, 49, 27, 7, 66, 53, 170, 63, 3, 172, 177, 176, 255, 249, 116, 172, 165, 78, 64, 218, 147, 214, 206, 68, 42, 186, 119, 75, 28, 141, 187, 117, 21, 89, 69, 96, 79, 211, 1, 141]
data = bytes(data)
encoded = base64.b64encode(data)
res = 'Qm6RErAN/8qtbbLCq8aPGHEuRl5HjJ+/hua+4N9e2RTximh4+VuGMGwxAPnr4eS+P8zYZpkzT93q/OecSheDoaydGg9YHBWqVrGx+W/mI7Q9jCEOSu79E7FM+RUjaRiIu3lHyu/rR348JVO6ZnJf1FEwZqfQQvqEx4mN537bfQFWV4ShN6bAG18b7eEg8Oqg94/x6MN1U4VFsu97kKwiKziIuERBRj2kbYaOmX2aPnWmVuonSc9DW1jcK5TJuYBdl9KnUlf2q33SLjycBK3blRjiP7BcZ37J/ga66aWp7Y38AMPU3rpnD4kp+xCjFrHozToyzVn5Ji1iKpsh4egQnVv2z6SW1kyXs8tDwtVTAmpt/g9uqBNyua4Uao103s2H3m5aGz0GdjKbBuDVbWL8VKZNfLu7ca0tEejQfvjvEiHNdSwg3wHd0ilDHNraodELXfpgAiud2Ya3GGmxStYScr9Aw17CE3PTZzHaVwjHMuGu3ksXn0w40OCsMMV+n79Q2JQecueOZJ9DTb5AthVsBOhJkffE3MXqN/HUc3OOrPiEdXNrsOaCvaCWP0/98HE93i5mdmTQqgA8mmao8Z+SRzf0e1IxQOe+MTMQb5nR0HQTRIvQafhQDO0dP1B/AXYWJ1MZ3EsfmBBe/o03Blkt9+XR79/ifDIz225k+3o1pj8rdL5yqUgSvjcE+QPIYwAlXjI6JTiaEpp/e7t7boMOuUzBC+MkuFgD3n4gj320aI5UFjUCJry7M6O9GddevsTVmxdUzu19TLkMb8n5ZTLZIAMlMbEECnsdfmps9lkqtocLmHoMF5/UNSz0MPuCbb9MlOJTN+FkxKarbFtD4s+PSVFfRVyNlmyo6wEhoJ4+lQDI5LAmcBL972vWERZw/3Wb+DtxZJFl9XHmpzrowzNMGgdeycZgXQjnPIslvyVlm1P2tW2V8WCofug25sWz1pRPDRvDpJK3gVJSsQL/CFXWU/Ttj2hrHNeyLkevuk2/XQ3MmurB5zEbB0I1qj8DrLGw//l0rKVOQNqT1s5EKrp3SxyNu3UVWUVgT9MBjQ=='
assert encoded.decode() == res
decoded = base64.b64decode(encoded)
assert decoded == data

View File

@ -1,50 +0,0 @@
import csv
def test(data: str, expected):
ret = list(csv.reader(data.splitlines()))
assert ret==expected, f"Expected {expected}, got {ret}"
test("""a,b,c
1,2,3
""", [['a', 'b', 'c'], ['1', '2', '3']])
test("""a,b,c
1,2,"3"
""", [['a', 'b', 'c'], ['1', '2', '3']])
test("""a,b,c
1,2,"3,,"
""", [['a', 'b', 'c'], ['1', '2', '3,,']])
test("""a,b,c
1,2,'3'
""", [['a', 'b', 'c'], ['1', '2', '\'3\'']])
test('''a,b,c
1,2,"123"""
''', [['a', 'b', 'c'], ['1', '2', '123"']])
test("""a,b,c,
1,2,3,
""", [['a', 'b', 'c', ''], ['1', '2', '3', '']])
test("""a,b ,c,
1,"22""33",3
""", [['a', 'b ', 'c', ''], ['1', '22"33', '3']])
# newline
test('''a,b,c
1,2,"3,
4"
5,"a,""
b",7
''', [['a', 'b', 'c'], ['1', '2', '3,\n 4'], ['5', 'a,"\nb', '7']])
ret = csv.DictReader("""a,b,c
1,2,3
"4",5,6
""".splitlines())
assert list(ret)==[
{'a': '1', 'b': '2', 'c': '3'},
{'a': '4', 'b': '5', 'c': '6'},
]