color-convert 1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ #coding=UTF-8
2
+
3
+ from .color import (
4
+ hex_to_rgb,
5
+ hex_to_rgba,
6
+ ten_to_hex,
7
+ ten_to_rgb,
8
+ rgb_to_hex,
9
+ rgba_to_hex,
10
+ rgb_to_hsl,
11
+ hsl_to_rgb,
12
+ rgb_to_hsv,
13
+ hsv_to_rgb,
14
+ rgb_to_cmyk,
15
+ cmyk_to_rgb,
16
+ hex_to_hsl,
17
+ hex_to_hsv,
18
+ )
19
+
20
+ __all__ = [
21
+ 'hex_to_rgb',
22
+ 'hex_to_rgba',
23
+ 'ten_to_hex',
24
+ 'ten_to_rgb',
25
+ 'rgb_to_hex',
26
+ 'rgba_to_hex',
27
+ 'rgb_to_hsl',
28
+ 'hsl_to_rgb',
29
+ 'rgb_to_hsv',
30
+ 'hsv_to_rgb',
31
+ 'rgb_to_cmyk',
32
+ 'cmyk_to_rgb',
33
+ 'hex_to_hsl',
34
+ 'hex_to_hsv',
35
+ ]
color_convert/color.py ADDED
@@ -0,0 +1,270 @@
1
+ #coding=UTF-8
2
+
3
+ def hex_to_rgb(hex):
4
+ """
5
+ 功能:16进制颜色格式颜色转换为RGB格式
6
+ :param hex: 16进制颜色值,如:#fff000
7
+ :return: rgb(255,182,193)
8
+ """
9
+ r = int(hex[1:3], 16)
10
+ g = int(hex[3:5], 16)
11
+ b = int(hex[5:7], 16)
12
+ rgb = str(r) + ',' + str(g) + ',' + str(b)
13
+ return 'rgb({})'.format(rgb)
14
+
15
+
16
+ def hex_to_rgba(hex_str):
17
+ """
18
+ 功能:带透明度的16进制颜色值转换为 RGBA 格式
19
+ :param hex_str: #eefff3a8 转换成 rgba(238,255,243,0.66) 或者 #ffffff 转换成 rgba(255,255,255,1)
20
+ :return: rgba(255,255,255,1)
21
+ """
22
+ hex_str = hex_str.replace('#', '')
23
+ if len(hex_str) != 6:
24
+ hex_str = hex_str + '0'
25
+ r = int(hex_str[0:2], 16)
26
+ g = int(hex_str[2:4], 16)
27
+ b = int(hex_str[4:6], 16)
28
+ a = 1
29
+ if len(hex_str) > 6:
30
+ alpha = int(hex_str[6:10], 16) / 255 * 1
31
+ a = round(alpha, 2)
32
+ return 'rgba({},{},{},{})'.format(r, g, b, a)
33
+
34
+
35
+ def ten_to_hex(num):
36
+ """
37
+ 功能:十进制颜色格式转换为十六进制格式。
38
+ :param num: 十进制颜色值,如:16711680
39
+ :return: #ffffff
40
+ """
41
+ color = str(hex(int(num)))
42
+ return color.replace('0x', '#')
43
+
44
+
45
+ def ten_to_rgb(num):
46
+ """
47
+ 功能:十进制颜色值转换为RGB格式
48
+ :param num: 十进制颜色值,如:16711680
49
+ :return: 255,0,0
50
+ """
51
+ try:
52
+ str1 = hex(int(str(num), 10)).replace('0x', '#')
53
+ return hex_to_rgba(str1)
54
+ except Exception as e:
55
+ raise Exception(e)
56
+
57
+
58
+ def rgb_to_hex(r, g, b):
59
+ """
60
+ 功能:RGB格式颜色转换为十六进制格式
61
+ :param r: 红色通道值 (0-255)
62
+ :param g: 绿色通道值 (0-255)
63
+ :param b: 蓝色通道值 (0-255)
64
+ :return: #ffffff
65
+ """
66
+ return '#{:02x}{:02x}{:02x}'.format(int(r), int(g), int(b))
67
+
68
+
69
+ def rgba_to_hex(r, g, b, a=1):
70
+ """
71
+ 功能:RGBA格式颜色转换为十六进制格式(带透明度)
72
+ :param r: 红色通道值 (0-255)
73
+ :param g: 绿色通道值 (0-255)
74
+ :param b: 蓝色通道值 (0-255)
75
+ :param a: 透明度 (0-1)
76
+ :return: #ffffffa8
77
+ """
78
+ alpha = int(round(a * 255))
79
+ return '#{:02x}{:02x}{:02x}{:02x}'.format(int(r), int(g), int(b), alpha)
80
+
81
+
82
+ def rgb_to_hsl(r, g, b):
83
+ """
84
+ 功能:RGB格式颜色转换为HSL格式
85
+ :param r: 红色通道值 (0-255)
86
+ :param g: 绿色通道值 (0-255)
87
+ :param b: 蓝色通道值 (0-255)
88
+ :return: hsl(120, 100%, 50%)
89
+ """
90
+ r, g, b = int(r) / 255.0, int(g) / 255.0, int(b) / 255.0
91
+ max_c = max(r, g, b)
92
+ min_c = min(r, g, b)
93
+ l = (max_c + min_c) / 2.0
94
+
95
+ if max_c == min_c:
96
+ h = s = 0.0
97
+ else:
98
+ d = max_c - min_c
99
+ s = d / (2.0 - max_c - min_c) if l > 0.5 else d / (max_c + min_c)
100
+ if max_c == r:
101
+ h = (g - b) / d + (6 if g < b else 0)
102
+ elif max_c == g:
103
+ h = (b - r) / d + 2
104
+ else:
105
+ h = (r - g) / d + 4
106
+ h /= 6.0
107
+
108
+ return 'hsl({}, {}%, {}%)'.format(round(h * 360), round(s * 100), round(l * 100))
109
+
110
+
111
+ def hsl_to_rgb(h, s, l):
112
+ """
113
+ 功能:HSL格式颜色转换为RGB格式
114
+ :param h: 色相 (0-360)
115
+ :param s: 饱和度 (0-100)
116
+ :param l: 亮度 (0-100)
117
+ :return: rgb(255,255,255)
118
+ """
119
+ h, s, l = float(h) / 360.0, float(s) / 100.0, float(l) / 100.0
120
+
121
+ if s == 0:
122
+ r = g = b = l
123
+ else:
124
+ def hue_to_rgb(p, q, t):
125
+ if t < 0: t += 1
126
+ if t > 1: t -= 1
127
+ if t < 1/6: return p + (q - p) * 6 * t
128
+ if t < 1/2: return q
129
+ if t < 2/3: return p + (q - p) * (2/3 - t) * 6
130
+ return p
131
+
132
+ q = l * (1 + s) if l < 0.5 else l + s - l * s
133
+ p = 2 * l - q
134
+ r = hue_to_rgb(p, q, h + 1/3)
135
+ g = hue_to_rgb(p, q, h)
136
+ b = hue_to_rgb(p, q, h - 1/3)
137
+
138
+ return 'rgb({}, {}, {})'.format(round(r * 255), round(g * 255), round(b * 255))
139
+
140
+
141
+ def rgb_to_hsv(r, g, b):
142
+ """
143
+ 功能:RGB格式颜色转换为HSV格式
144
+ :param r: 红色通道值 (0-255)
145
+ :param g: 绿色通道值 (0-255)
146
+ :param b: 蓝色通道值 (0-255)
147
+ :return: hsv(120, 100%, 100%)
148
+ """
149
+ r, g, b = int(r) / 255.0, int(g) / 255.0, int(b) / 255.0
150
+ max_c = max(r, g, b)
151
+ min_c = min(r, g, b)
152
+ v = max_c
153
+ d = max_c - min_c
154
+ s = 0 if max_c == 0 else d / max_c
155
+
156
+ if max_c == min_c:
157
+ h = 0.0
158
+ else:
159
+ if max_c == r:
160
+ h = (g - b) / d + (6 if g < b else 0)
161
+ elif max_c == g:
162
+ h = (b - r) / d + 2
163
+ else:
164
+ h = (r - g) / d + 4
165
+ h /= 6.0
166
+
167
+ return 'hsv({}, {}%, {}%)'.format(round(h * 360), round(s * 100), round(v * 100))
168
+
169
+
170
+ def hsv_to_rgb(h, s, v):
171
+ """
172
+ 功能:HSV格式颜色转换为RGB格式
173
+ :param h: 色相 (0-360)
174
+ :param s: 饱和度 (0-100)
175
+ :param v: 明度 (0-100)
176
+ :return: rgb(255,255,255)
177
+ """
178
+ h, s, v = float(h) / 360.0, float(s) / 100.0, float(v) / 100.0
179
+
180
+ i = int(h * 6)
181
+ f = h * 6 - i
182
+ p = v * (1 - s)
183
+ q = v * (1 - f * s)
184
+ t = v * (1 - (1 - f) * s)
185
+
186
+ mod = i % 6
187
+ if mod == 0:
188
+ r, g, b = v, t, p
189
+ elif mod == 1:
190
+ r, g, b = q, v, p
191
+ elif mod == 2:
192
+ r, g, b = p, v, t
193
+ elif mod == 3:
194
+ r, g, b = p, q, v
195
+ elif mod == 4:
196
+ r, g, b = t, p, v
197
+ else:
198
+ r, g, b = v, p, q
199
+
200
+ return 'rgb({}, {}, {})'.format(round(r * 255), round(g * 255), round(b * 255))
201
+
202
+
203
+ def rgb_to_cmyk(r, g, b):
204
+ """
205
+ 功能:RGB格式颜色转换为CMYK格式
206
+ :param r: 红色通道值 (0-255)
207
+ :param g: 绿色通道值 (0-255)
208
+ :param b: 蓝色通道值 (0-255)
209
+ :return: cmyk(0%, 0%, 0%, 0%)
210
+ """
211
+ r, g, b = int(r) / 255.0, int(g) / 255.0, int(b) / 255.0
212
+ k = 1 - max(r, g, b)
213
+ if k == 1:
214
+ return 'cmyk(0%, 0%, 0%, 100%)'
215
+ c = (1 - r - k) / (1 - k)
216
+ m = (1 - g - k) / (1 - k)
217
+ y = (1 - b - k) / (1 - k)
218
+ return 'cmyk({}%, {}%, {}%, {}%)'.format(round(c * 100), round(m * 100), round(y * 100), round(k * 100))
219
+
220
+
221
+ def cmyk_to_rgb(c, m, y, k):
222
+ """
223
+ 功能:CMYK格式颜色转换为RGB格式
224
+ :param c: 青色 (0-100)
225
+ :param m: 品红 (0-100)
226
+ :param y: 黄色 (0-100)
227
+ :param k: 黑色 (0-100)
228
+ :return: rgb(255,255,255)
229
+ """
230
+ c, m, y, k = float(c) / 100.0, float(m) / 100.0, float(y) / 100.0, float(k) / 100.0
231
+ r = round(255 * (1 - c) * (1 - k))
232
+ g = round(255 * (1 - m) * (1 - k))
233
+ b = round(255 * (1 - y) * (1 - k))
234
+ return 'rgb({}, {}, {})'.format(r, g, b)
235
+
236
+
237
+ def hex_to_hsl(hex_str):
238
+ """
239
+ 功能:十六进制颜色格式转换为HSL格式
240
+ :param hex_str: 16进制颜色值,如:#fff000
241
+ :return: hsl(56, 100%, 50%)
242
+ """
243
+ hex_str = hex_str.replace('#', '')
244
+ r = int(hex_str[0:2], 16)
245
+ g = int(hex_str[2:4], 16)
246
+ b = int(hex_str[4:6], 16)
247
+ return rgb_to_hsl(r, g, b)
248
+
249
+
250
+ def hex_to_hsv(hex_str):
251
+ """
252
+ 功能:十六进制颜色格式转换为HSV格式
253
+ :param hex_str: 16进制颜色值,如:#fff000
254
+ :return: hsv(56, 100%, 100%)
255
+ """
256
+ hex_str = hex_str.replace('#', '')
257
+ r = int(hex_str[0:2], 16)
258
+ g = int(hex_str[2:4], 16)
259
+ b = int(hex_str[4:6], 16)
260
+ return rgb_to_hsv(r, g, b)
261
+
262
+
263
+ def my_function(a, b):
264
+ """函数功能说明
265
+ >>> my_function(2, 3)
266
+ 6
267
+ >>> my_function('a', 3)
268
+ 'aaa'
269
+ """
270
+ return a * b
@@ -0,0 +1,207 @@
1
+ Metadata-Version: 2.4
2
+ Name: color-convert
3
+ Version: 1.2
4
+ Summary: Color Conversion
5
+ Home-page: https://github.com/zhenzi0322/color-convert
6
+ Author: zhenzi0322
7
+ Author-email: 82131529@qq.com
8
+ License: MIT License
9
+ Keywords: color,color-convert,convert
10
+ Classifier: Programming Language :: Python :: 3.7
11
+ Classifier: Operating System :: MacOS :: MacOS X
12
+ Classifier: Operating System :: POSIX :: Linux
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Dynamic: author
16
+ Dynamic: author-email
17
+ Dynamic: classifier
18
+ Dynamic: description
19
+ Dynamic: description-content-type
20
+ Dynamic: home-page
21
+ Dynamic: keywords
22
+ Dynamic: license
23
+ Dynamic: license-file
24
+ Dynamic: summary
25
+
26
+
27
+ <p align="center">
28
+ <h1>color-convert</h1>
29
+ <b>颜色格式转换工具,支持 HEX、RGB、RGBA、HSL、HSV、CMYK、十进制等多种颜色格式之间的相互转换。</b>
30
+ <br><br>
31
+ <a href="https://pypi.org/project/color-convert/"><img src="https://img.shields.io/pypi/v/color-convert.svg" alt="PyPI version"></a>
32
+ <a href="https://pypi.org/project/color-convert/"><img src="https://img.shields.io/pypi/pyversions/color-convert.svg" alt="Python"></a>
33
+ <a href="https://github.com/zhenzi0322/color-convert/blob/main/LICENSE"><img src="https://img.shields.io/pypi/l/color-convert.svg" alt="License"></a>
34
+ <a href="https://tool.long920.cn/color-convert"><img src="https://app.readthedocs.org/projects/zhenzi0322-tool/badge/?version=latest" alt="Documentation Status"></a>
35
+ </p>
36
+
37
+ ## 安装
38
+
39
+ ```bash
40
+ pip install color-convert
41
+ ```
42
+
43
+ ## 支持的颜色格式
44
+
45
+ | 格式 | 示例 |
46
+ |------|------|
47
+ | HEX(十六进制) | `#fff000` |
48
+ | RGB | `rgb(255, 240, 0)` |
49
+ | RGBA | `rgba(255, 240, 0, 1)` |
50
+ | HSL | `hsl(56, 100%, 50%)` |
51
+ | HSV | `hsv(56, 100%, 100%)` |
52
+ | CMYK | `cmyk(0%, 6%, 100%, 0%)` |
53
+ | Decimal(十进制) | `16711680` |
54
+
55
+ ## 使用示例
56
+
57
+ ### 十六进制 → RGB
58
+
59
+ ```python
60
+ from color_convert import color
61
+
62
+ rgb = color.hex_to_rgb('#fff000')
63
+ print(rgb) # rgb(255,240,0)
64
+ ```
65
+
66
+ ### 十六进制 → RGBA
67
+
68
+ ```python
69
+ from color_convert import color
70
+
71
+ rgba = color.hex_to_rgba('#fff000')
72
+ print(rgba) # rgba(255,240,0,1)
73
+
74
+ rgba = color.hex_to_rgba('#fff000a8')
75
+ print(rgba) # rgba(255,240,0,0.66)
76
+ ```
77
+
78
+ ### 十六进制 → HSL
79
+
80
+ ```python
81
+ from color_convert import color
82
+
83
+ hsl = color.hex_to_hsl('#fff000')
84
+ print(hsl) # hsl(56, 100%, 50%)
85
+ ```
86
+
87
+ ### 十六进制 → HSV
88
+
89
+ ```python
90
+ from color_convert import color
91
+
92
+ hsv = color.hex_to_hsv('#fff000')
93
+ print(hsv) # hsv(56, 100%, 100%)
94
+ ```
95
+
96
+ ### 十进制 → 十六进制
97
+
98
+ ```python
99
+ from color_convert import color
100
+
101
+ hex_val = color.ten_to_hex(16711680)
102
+ print(hex_val) # #ff0000
103
+ ```
104
+
105
+ ### 十进制 → RGBA
106
+
107
+ ```python
108
+ from color_convert import color
109
+
110
+ rgba = color.ten_to_rgb(16711680)
111
+ print(rgba) # rgba(255,0,0,1)
112
+ ```
113
+
114
+ ### RGB → 十六进制
115
+
116
+ ```python
117
+ from color_convert import color
118
+
119
+ hex_val = color.rgb_to_hex(255, 240, 0)
120
+ print(hex_val) # #fff000
121
+ ```
122
+
123
+ ### RGBA → 十六进制
124
+
125
+ ```python
126
+ from color_convert import color
127
+
128
+ hex_val = color.rgba_to_hex(255, 240, 0, 0.66)
129
+ print(hex_val) # #fff000a8
130
+ ```
131
+
132
+ ### RGB → HSL
133
+
134
+ ```python
135
+ from color_convert import color
136
+
137
+ hsl = color.rgb_to_hsl(255, 240, 0)
138
+ print(hsl) # hsl(56, 100%, 50%)
139
+ ```
140
+
141
+ ### HSL → RGB
142
+
143
+ ```python
144
+ from color_convert import color
145
+
146
+ rgb = color.hsl_to_rgb(56, 100, 50)
147
+ print(rgb) # rgb(255, 238, 0)
148
+ ```
149
+
150
+ ### RGB → HSV
151
+
152
+ ```python
153
+ from color_convert import color
154
+
155
+ hsv = color.rgb_to_hsv(255, 240, 0)
156
+ print(hsv) # hsv(56, 100%, 100%)
157
+ ```
158
+
159
+ ### HSV → RGB
160
+
161
+ ```python
162
+ from color_convert import color
163
+
164
+ rgb = color.hsv_to_rgb(56, 100, 100)
165
+ print(rgb) # rgb(255, 238, 0)
166
+ ```
167
+
168
+ ### RGB → CMYK
169
+
170
+ ```python
171
+ from color_convert import color
172
+
173
+ cmyk = color.rgb_to_cmyk(255, 240, 0)
174
+ print(cmyk) # cmyk(0%, 6%, 100%, 0%)
175
+ ```
176
+
177
+ ### CMYK → RGB
178
+
179
+ ```python
180
+ from color_convert import color
181
+
182
+ rgb = color.cmyk_to_rgb(0, 6, 100, 0)
183
+ print(rgb) # rgb(255, 240, 0)
184
+ ```
185
+
186
+ ## API 参考
187
+
188
+ | 函数名 | 说明 | 参数 | 返回值示例 |
189
+ |--------|------|------|------------|
190
+ | `hex_to_rgb(hex)` | 十六进制 → RGB | `hex`: 如 `#fff000` | `rgb(255,240,0)` |
191
+ | `hex_to_rgba(hex_str)` | 十六进制 → RGBA(支持带透明度) | `hex_str`: 如 `#fff000` 或 `#fff000a8` | `rgba(255,240,0,1)` |
192
+ | `hex_to_hsl(hex_str)` | 十六进制 → HSL | `hex_str`: 如 `#fff000` | `hsl(56, 100%, 50%)` |
193
+ | `hex_to_hsv(hex_str)` | 十六进制 → HSV | `hex_str`: 如 `#fff000` | `hsv(56, 100%, 100%)` |
194
+ | `ten_to_hex(num)` | 十进制 → 十六进制 | `num`: 如 `16711680` | `#ff0000` |
195
+ | `ten_to_rgb(num)` | 十进制 → RGBA | `num`: 如 `16711680` | `rgba(255,0,0,1)` |
196
+ | `rgb_to_hex(r, g, b)` | RGB → 十六进制 | `r, g, b`: 0-255 | `#fff000` |
197
+ | `rgba_to_hex(r, g, b, a)` | RGBA → 十六进制(带透明度) | `r, g, b`: 0-255, `a`: 0-1 | `#fff000a8` |
198
+ | `rgb_to_hsl(r, g, b)` | RGB → HSL | `r, g, b`: 0-255 | `hsl(56, 100%, 50%)` |
199
+ | `hsl_to_rgb(h, s, l)` | HSL → RGB | `h`: 0-360, `s`: 0-100, `l`: 0-100 | `rgb(255, 238, 0)` |
200
+ | `rgb_to_hsv(r, g, b)` | RGB → HSV | `r, g, b`: 0-255 | `hsv(56, 100%, 100%)` |
201
+ | `hsv_to_rgb(h, s, v)` | HSV → RGB | `h`: 0-360, `s`: 0-100, `v`: 0-100 | `rgb(255, 238, 0)` |
202
+ | `rgb_to_cmyk(r, g, b)` | RGB → CMYK | `r, g, b`: 0-255 | `cmyk(0%, 6%, 100%, 0%)` |
203
+ | `cmyk_to_rgb(c, m, y, k)` | CMYK → RGB | `c, m, y, k`: 0-100 | `rgb(255, 240, 0)` |
204
+
205
+ ## License
206
+
207
+ [MIT License](LICENSE)
@@ -0,0 +1,7 @@
1
+ color_convert/__init__.py,sha256=bVeDVYLJ6nxfKwg4P9dF2x1OFc1h40Nr0tas09z2GCE,537
2
+ color_convert/color.py,sha256=7Uws9AHbAVFNAT73g4p3kc8bpcRMtnjn44XjlM6V9b4,7283
3
+ color_convert-1.2.dist-info/licenses/LICENSE,sha256=MK0YzZf6O8ILUNhytU5rNZMl2_2ECw2AnOvb4b7eMuA,1067
4
+ color_convert-1.2.dist-info/METADATA,sha256=iPpgIciN2-H2qxqD-B19atFpNHxizoTt4Oe5kvGizxs,5311
5
+ color_convert-1.2.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
6
+ color_convert-1.2.dist-info/top_level.txt,sha256=TxEO_A-PV4am8TGe4tHTow6QKN4ZX6dkpuyLip1Sl8s,14
7
+ color_convert-1.2.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (83.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 zhenzi0322
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ color_convert