https://github.com/magarcia/python-x256
Find the nearest xterm 256 color index for an RGB.
https://github.com/magarcia/python-x256
x256 xterm
Last synced: 26 days ago
JSON representation
Find the nearest xterm 256 color index for an RGB.
- Host: GitHub
- URL: https://github.com/magarcia/python-x256
- Owner: magarcia
- License: mit
- Created: 2012-05-12T22:14:13.000Z (about 13 years ago)
- Default Branch: main
- Last Pushed: 2022-01-30T09:34:47.000Z (over 3 years ago)
- Last Synced: 2025-05-02T03:45:59.395Z (about 1 month ago)
- Topics: x256, xterm
- Language: Python
- Homepage:
- Size: 26.4 KB
- Stars: 25
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# x256 [](https://github.com/magarcia/python-x256/actions/workflows/test.yml) [](https://github.com/magarcia/python-x256/actions/workflows/codeql-analysis.yml) [](https://pypi.org/project/x256/)
_This is a python version from [node-x256](https://github.com/substack/node-x256)_
Return the nearest
[xterm 256 color code](http://www.frexx.de/xterm-256-notes/)
for rgb inputs.# Example
You can just print the index:
```python
from x256 import x256
ix = x256.from_rgb(220, 40, 150)
print ix # 162
```or you can use raw ansi escape codes:
```python
from x256 import x256
ix = x256.from_rgb(220, 40, 150)
print '\x1b[38;5;' + str(ix) + 'mBEEEEEP'
```output:

# Methods
## x256.from_rgb(red, green, blue)
Return the nearest xterm 256 color code for the 24-bit `[red, green, blue]`
values.`red`, `green`, and `blue` should each be integers from 0 through 255,
inclusive.## x256.from_hex(hex)
Return the nearest xterm 256 color code for the hexadecimal color
values.`hex` should be string without 0x.
## x256.to_rgb(ix)
Return 24-bit `[red, green, blue]` values from xterm 256 color code.
`ix` should be integer.
## x256.to_hex(ix)
Return hexadecimal color from xterm 256 color code.
`ix` should be integer.
# Install
```shell
python -m pip install x256
```# License
Copyright (c) 2012 Martin Garcia ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.