Metadata-Version: 2.3
Name: colex
Version: 0.3.0
Summary: Library with constants and functions for working with colors
Project-URL: Source, https://github.com/havsalt/colex.git
Author-email: Havsalt <77575424+Havsalt@users.noreply.github.com>
License: MIT License
        
        Copyright (c) 2025 Havsalt
        
        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.
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# Colex

Library with constants and functions for working with colors

## Installation

Install using either `pip` or `rye`:

```bash
pip install colex
```

```bash
rye install colex
```

## Getting started

```python
import colex  # Every public namespace is available under `colex`

print(colex.RED + "Hello red!")
print("Still red...")
print(colex.RESET, "Back to normal")

# Optionally, you can import submodules or constants like this
from colex import color, style, RESET
print(style.ITALIC + color.GREEN + "Hello italic green" + RESET)

# You may want to use this helper function to have color param at the end
from colex import colorize

print(colorize("Hello blue!", colex.BLUE))

# Note that `colex` is using ANSI escape codes,
# therefore any string can be annotated with `ColorValue`
from colex import ColorValue

my_color: ColorValue = "\x1b[31m"  # The ANSI code for red
print(my_color + "Hello red, again")

# `ColorValue` is both an annotation and protocol
# This is to support custom 3rd party classes that implements `__str__`
class Color:
    def __init__(self, code: int) -> int:
        self.code = code

    def __str__(self) -> str:
        return colex.from_ansi(code)

yellow_color: ColorValue = Color(33)  # Code for Yellow
color_string = str(yellow_color)  # Implements proper `__str__`
# You may call either `colorize` or explicit str(...) like this
print(colorize("Hello yellow", yellow_color))
print(str(yellow_color) + "Hello yellow" + RESET)
```

## Rational

Originally, using colors in my projects was done by simply printing `"\x1b[31m"` and such. Eventually, the color strings were assigned to constants with understandable names, like `RED`. They were put in a seperate module, a `color.py`, that was copied around projects when I needed to do colors. After some time, I found it messy when it ended up looking:

```python
import color
from color import ColorValue

class Color:  # This was used as a mixin component
    color: ColorValue = color.RED  # Too many occurances of "color" for me to stay sane
```

It was then nice in itself to distinguish the namespace `colex` from `color`. I then ended up with:

```python
import colex
from colex import ColorValue

class Color:
    color: ColorValue = colex.RED
```

Having a different namespace was nice, but the main advantage was having `colex` on `PyPI`. This way, I didn't need to copy over a `color.py` file everytime, but I could instead just install it using the desired package manager.

It also became easier to develop `charz`, as the color aspect was split into it's own package.

## Includes

- Annotations
  - `ColorValue`
  - `ColorCode`
  - `HexCode`
- Functions
  - `colorize`
  - `from_ansi`
  - `from_hex`
  - `from_random`
  - `from_rgb`
- Constants
  - `NONE`
  - `RESET`
- Modules with constants
  - `color`
    - HTML/CSS named colors
  - `style`
    - `BOLD`
    - `FAINT`
    - `ITALIC`
    - `UNDERLINE`
    - `BLINK`
    - `RAPID_BLINK`
    - `REVERSE`
    - `CONCEAL`
    - `STRIKETHROUGH`

## License

MIT
