colourings


Namecolourings JSON
Version 0.2.3 PyPI version JSON
download
home_pageNone
SummaryColouring in Python
upload_time2025-02-26 09:06:12
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseBSD-3-Clause license Copyright (c) 2025, Daniel Stoops All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
keywords color colour
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # colourings
[![PyPI - Version](https://img.shields.io/pypi/v/colourings)](https://pypi.org/project/colourings/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/colourings)
[![codecov](https://codecov.io/github/Stoops-ML/colourings/graph/badge.svg?token=NQUPC3NY6S)](https://codecov.io/github/Stoops-ML/colourings)

Converts and manipulates common color representation (RGB, HSL, web, ...)

This is a fork of the [Colour](https://github.com/vaab/colour/) Python package. This fork contains a few changes:
- Support for RGBA and HSLA [#42](https://github.com/vaab/colour/issues/42)
- Add Teal [#58](https://github.com/vaab/colour/issues/58) and RebeccaPurple colors [#25](https://github.com/vaab/colour/pull/25)
- Correct luminance and lightness definitions [#64](https://github.com/vaab/colour/issues/64)
- Add `Colour` class [#46](https://github.com/vaab/colour/pull/46)
- Add `Color.preview` method [#63](https://github.com/vaab/colour/pull/63)
- Color scale uses shortest path [#50](https://github.com/vaab/colour/pull/50)
- `color_scale` can interpolate between more than two colors
- RGB values are between 0 and 255 and hue is between 0 and 360. This is in contrast to RGBA and HSLA values that are between 0 and 1
- Add typing
- Updated project structure

Below is a modified copy of the readme from [Colour](https://github.com/vaab/colour/).

## Features

- Damn simple and pythonic way to manipulate color representation (see examples below)
- Full conversion between RGB, HSL, 6-digit hex, 3-digit hex, human color
- One object (`Color`) or bunch of single purpose function (`rgb2hex`, `hsl2rgb` ...)
- `web` format that use the smallest representation between 6-digit (e.g. `#fa3b2c`), 3-digit (e.g. `#fbb`), fully spelled color (e.g. `white`), following W3C color naming for compatible CSS or HTML color specifications.
- Smooth intuitive color scale generation choosing N color gradients.
- Can pick colors for you to identify objects of your application.

## Installation

```
pip install colourings
```

## Usage

To get complete demo of each function, please read the source code which is heavily documented and provide a lot of examples in doctest format.

Here is a reduced sample of a common usage scenario:

### Instantiation

Let's create blue color:

```
>>> from colourings import Color
>>> c = Color("blue")
>>> c
<Color blue>
```

Please note that all of these are equivalent examples to create the red color:

```
Color("red")               ## human, web compatible representation
Color("blue", hue=0)       ## hue of blue is 0.66, hue of red is 0.0
Color("#f00")              ## standard 3 hex digit web compatible representation
Color("#ff0000")           ## standard 6 hex digit web compatible representation
Color(hsl=(0, 1, 0.5))     ## full 3-uple HSL specification
Color(hsla=(0, 1, 0.5, 1)) ## full 4-uple HSLA specification
Color(rgb=(255, 0, 0))     ## full 3-uple RGB specification
Color(rgba=(1, 0, 0, 1))   ## full 4-uple RGBA specification
Color(Color("red"))        ## recursion doesn't break object
```

### Reading Values

Several representations are accessible:

```
>>> c.hex
'#00f'
>>> c.hsl
(0.66..., 1.0, 0.5)
>>> c.rgb
(0.0, 0.0, 1.0)
```

And their different parts are also independently accessible, as the different amount of red, blue, green, in the RGB format:

```
>>> c.red
0.0
>>> c.blue
1.0
>>> c.green
0.0
```

Or the hue, saturation and lightness of the HSL representation:

```
>>> c.hue
0.66...
>>> c.saturation
1.0
>>> c.lightness
0.5
```

A note on the `.hex` property, it'll return the smallest valid value when possible. If you are only interested by the long value, use `.hex_l`:

```
>>> c.hex_l
'#0000ff'
```

### Modifying Color Objects

All of these properties are read/write, so let's add some red to this color:

```
>>> c.red = 1
>>> c
<Color magenta>
```

We might want to de-saturate this color:

```
>>> c.saturation = 0.5
>>> c
<Color #bf40bf>
```

And of course, the string conversion will give the web representation which is human, or 3-digit, or 6-digit hex representation depending which is usable:

```
>>> f"{c}"
'#bf40bf'

>>> c.lightness = 1
>>> f"{c}"
'white'
```

### Ranges of Colors

You can get some color scale of variation between two `Color` objects quite easily. Here, is the color scale of the rainbow between red and blue:

```
>>> red = Color("red")
>>> blue = Color("blue")
>>> list(red.range_to(blue, 3))
[<Color red>, <Color magenta>, <Color blue>]
>>> list(red.range_to(blue, 5, longer=True))
[<Color red>, <Color yellow>, <Color lime>, <Color cyan>, <Color blue>]
```

Or the different amount of gray between black and white:

```
>>> black = Color("black")
>>> white = Color("white")
>>> list(black.range_to(white, 6))
[<Color black>, <Color #333>, <Color #666>, <Color #999>, <Color #ccc>, <Color white>]
```

If you have to create graphical representation with color scale between red and green ('lime' color is full green):

```
>>> lime = Color("lime")
>>> list(red.range_to(lime, 5))
[<Color red>, <Color #ff7f00>, <Color yellow>, <Color chartreuse>, <Color lime>]
```

Notice how naturally, the yellow is displayed in human format and in the middle of the scale. And that the quite unusual (but compatible) 'chartreuse' color specification has been used in place of the hexadecimal representation.

You can create your own scale with as many colors as you like:
```
>>> from colourings import color_scale
>>> color_scale((Color("black"), Color("orange"), Color("blue"), Color("white")), 10)
[Color("black"), Color("#39221c"), Color("#8e4d1c"), Color("orange"), Color("#ff003c"), Color("#e100ff"), Color("blue"), Color("#bd71e3"), Color("#e3c6d9"), Color("white")]
```

### Color Comparison

#### Sane Default

Color comparison is a vast subject. However, it might seem quite straightforward for you. `Color` uses a configurable default way of comparing color that might suit your needs:

```
>>> Color("red") == Color("#f00") == Color("blue", hue=0)
True
```

The default comparison algorithm focuses only on the "web" representation which is equivalent to comparing the long hex representation (e.g. #FF0000) or to be more specific, it is equivalent to compare the amount of red, green, and blue composition of the RGB representation, each of these value being quantized to a 256 value scale.

This default comparison is a practical and convenient way to measure the actual color equivalence on your screen, or in your video card memory.

But this comparison wouldn't make the difference between a black red, and a black blue, which both are black:

```
>>> black_red = Color("red", lightness=0)
>>> black_blue = Color("blue", lightness=0)

>>> black_red == black_blue
True
```

#### Customization

But, this is not the sole way to compare two colors. As I'm quite lazy, I'm providing you a way to customize it to your needs. Thus:

```
>>> from colourings import RGB_equivalence, HSL_equivalence
>>> black_red = Color("red", lightness=0, equality=HSL_equivalence)
>>> black_blue = Color("blue", lightness=0, equality=HSL_equivalence)

>>> black_red == black_blue
False
```

As you might have already guessed, the sane default is `RGB_equivalence`, so:

```
>>> black_red = Color("red", lightness=0, equality=RGB_equivalence)
>>> black_blue = Color("blue", lightness=0, equality=RGB_equivalence)

>>> black_red == black_blue
True
```

Here's how you could implement your unique comparison function:

```
>>> saturation_equivalence = lambda c1, c2: c1.saturation == c2.saturation
>>> red = Color("red", equality=saturation_equivalence)
>>> blue = Color("blue", equality=saturation_equivalence)
>>> white = Color("white", equality=saturation_equivalence)

>>> red == blue
True
>>> white == red
False
```

Note: When comparing 2 colors, only the equality function of the first color will be used. Thus:

```
>>> black_red = Color("red", luminance=0, equality=RGB_equivalence)
>>> black_blue = Color("blue", luminance=0, equality=HSL_equivalence)

>>> black_red == black_blue
True
```
But reverse operation is not equivalent !:

```
>>> black_blue == black_red
False
```

### Equality to Non-`Color` Objects
As a side note, whatever your custom equality function is, it won't be used if you compare to anything else than a `Color` instance:

```
>>> red = Color("red", equality=lambda c1, c2: True)
>>> blue = Color("blue", equality=lambda c1, c2: True)
```
Note that these instances would compare as equal to any other color:

```
>>> red == blue
True
```

But on another non-`Color` object:

```
>>> red == None
False
>>> red != None
True
```

Actually, `Color` instances will, politely enough, leave the other side of the equality have a chance to decide of the output, (by executing its own __eq__), so:

```
>>> class OtherColorImplem():
...     def __init__(self, color):
...         self.color = color
...     def __eq__(self, other):
...         return self.color == other.web

>>> alien_red = OtherColorImplem("red")
>>> red == alien_red
True
>>> blue == alien_red
False
```

And inequality (using __ne__) are also polite:

```
>>> class AnotherColorImplem(OtherColorImplem):
...     def __ne__(self, other):
...         return self.color != other.web

>>> new_alien_red = AnotherColorImplem("red")
>>> red != new_alien_red
False
>>> blue != new_alien_red
True
```

## Picking Arbitrary Color for a Python Object
### Basic Usage
Sometimes, you just want to pick a color for an object in your application often to visually identify this object. Thus, the picked color should be the same for same objects, and different for different object:

```
>>> foo = object()
>>> bar = object()

>>> Color(pick_for=foo)
<Color ...>
>>> Color(pick_for=foo) == Color(pick_for=foo)
True
>>> Color(pick_for=foo) == Color(pick_for=bar)
False
```

Of course, although there's a tiny probability that different strings yield the same color, most of the time, different inputs will produce different colors.

#### Advanced Usage
You can customize your color picking algorithm by providing a picker. A picker is a callable that takes an object, and returns something that can be instantiated as a color by Color:

```
>>> my_picker = lambda obj: "red" if isinstance(obj, int) else "blue"
>>> Color(pick_for=3, picker=my_picker, pick_key=None)
<Color red>
>>> Color(pick_for="foo", picker=my_picker, pick_key=None)
<Color blue>
```

You might want to use a particular picker, but enforce how the picker will identify two object as the same (or not). So there's a pick_key attribute that is provided and defaults as equivalent of hash method and if hash is not supported by your object, it'll default to the str of your object salted with the class name.

Thus:

```
>>> class MyObj(str): pass
>>> my_obj_color = Color(pick_for=MyObj("foo"))
>>> my_str_color = Color(pick_for="foo")
>>> my_obj_color == my_str_color
False
```

Please make sure your object is hashable or "stringable" before using the RGB_color_picker picking mechanism or provide another color picker. Nearly all python object are hashable by default so this shouldn't be an issue (e.g. instances of object and subclasses are hashable).

Neither hash nor str are perfect solution. So feel free to use pick_key at Color instantiation time to set your way to identify objects, for instance:

```
>>> a = object()
>>> b = object()
>>> Color(pick_for=a, pick_key=id) == Color(pick_for=b, pick_key=id)
False
```

When choosing a pick key, you should closely consider if you want your color to be consistent between runs (this is NOT the case with the last example), or consistent with the content of your object if it is a mutable object.

Default value of pick_key and picker ensures that the same color will be attributed to same object between different run on different computer for most python object.

### Color Factory
As you might have noticed, there are few attributes that you might want to see attached to all of your colors as equality for equality comparison support, or picker, pick_key to configure your object color picker.

You can create a customized Color factory thanks to the make_color_factory:

```
>>> from colourings import make_color_factory, HSL_equivalence, RGB_color_picker

>>> get_color = make_color_factory(
...    equality=HSL_equivalence,
...    picker=RGB_color_picker,
...    pick_key=str,
... )
```

All color created thanks to CustomColor class instead of the default one would get the specified attributes by default:

```
>>> black_red = get_color("red", luminance=0)
>>> black_blue = get_color("blue", luminance=0)
Of course, these are always instances of Color class:

>>> isinstance(black_red, Color)
True
Equality was changed from normal defaults, so:

>>> black_red == black_blue
False
```

This because the default equivalence of Color was set to HSL_equivalence.

## Contributing
Any suggestion or issue is welcome. Push request are very welcome, please check out the guidelines.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "colourings",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "color, colour",
    "author": null,
    "author_email": "Daniel Stoops <danielstoops25@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/44/e8/0418ef58e441a2f8cc05da8f5be9399e17623d6c2aa0b37e656e4912bd07/colourings-0.2.3.tar.gz",
    "platform": null,
    "description": "# colourings\n[![PyPI - Version](https://img.shields.io/pypi/v/colourings)](https://pypi.org/project/colourings/)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/colourings)\n[![codecov](https://codecov.io/github/Stoops-ML/colourings/graph/badge.svg?token=NQUPC3NY6S)](https://codecov.io/github/Stoops-ML/colourings)\n\nConverts and manipulates common color representation (RGB, HSL, web, ...)\n\nThis is a fork of the [Colour](https://github.com/vaab/colour/) Python package. This fork contains a few changes:\n- Support for RGBA and HSLA [#42](https://github.com/vaab/colour/issues/42)\n- Add Teal [#58](https://github.com/vaab/colour/issues/58) and RebeccaPurple colors [#25](https://github.com/vaab/colour/pull/25)\n- Correct luminance and lightness definitions [#64](https://github.com/vaab/colour/issues/64)\n- Add `Colour` class [#46](https://github.com/vaab/colour/pull/46)\n- Add `Color.preview` method [#63](https://github.com/vaab/colour/pull/63)\n- Color scale uses shortest path [#50](https://github.com/vaab/colour/pull/50)\n- `color_scale` can interpolate between more than two colors\n- RGB values are between 0 and 255 and hue is between 0 and 360. This is in contrast to RGBA and HSLA values that are between 0 and 1\n- Add typing\n- Updated project structure\n\nBelow is a modified copy of the readme from [Colour](https://github.com/vaab/colour/).\n\n## Features\n\n- Damn simple and pythonic way to manipulate color representation (see examples below)\n- Full conversion between RGB, HSL, 6-digit hex, 3-digit hex, human color\n- One object (`Color`) or bunch of single purpose function (`rgb2hex`, `hsl2rgb` ...)\n- `web` format that use the smallest representation between 6-digit (e.g. `#fa3b2c`), 3-digit (e.g. `#fbb`), fully spelled color (e.g. `white`), following W3C color naming for compatible CSS or HTML color specifications.\n- Smooth intuitive color scale generation choosing N color gradients.\n- Can pick colors for you to identify objects of your application.\n\n## Installation\n\n```\npip install colourings\n```\n\n## Usage\n\nTo get complete demo of each function, please read the source code which is heavily documented and provide a lot of examples in doctest format.\n\nHere is a reduced sample of a common usage scenario:\n\n### Instantiation\n\nLet's create blue color:\n\n```\n>>> from colourings import Color\n>>> c = Color(\"blue\")\n>>> c\n<Color blue>\n```\n\nPlease note that all of these are equivalent examples to create the red color:\n\n```\nColor(\"red\")               ## human, web compatible representation\nColor(\"blue\", hue=0)       ## hue of blue is 0.66, hue of red is 0.0\nColor(\"#f00\")              ## standard 3 hex digit web compatible representation\nColor(\"#ff0000\")           ## standard 6 hex digit web compatible representation\nColor(hsl=(0, 1, 0.5))     ## full 3-uple HSL specification\nColor(hsla=(0, 1, 0.5, 1)) ## full 4-uple HSLA specification\nColor(rgb=(255, 0, 0))     ## full 3-uple RGB specification\nColor(rgba=(1, 0, 0, 1))   ## full 4-uple RGBA specification\nColor(Color(\"red\"))        ## recursion doesn't break object\n```\n\n### Reading Values\n\nSeveral representations are accessible:\n\n```\n>>> c.hex\n'#00f'\n>>> c.hsl\n(0.66..., 1.0, 0.5)\n>>> c.rgb\n(0.0, 0.0, 1.0)\n```\n\nAnd their different parts are also independently accessible, as the different amount of red, blue, green, in the RGB format:\n\n```\n>>> c.red\n0.0\n>>> c.blue\n1.0\n>>> c.green\n0.0\n```\n\nOr the hue, saturation and lightness of the HSL representation:\n\n```\n>>> c.hue\n0.66...\n>>> c.saturation\n1.0\n>>> c.lightness\n0.5\n```\n\nA note on the `.hex` property, it'll return the smallest valid value when possible. If you are only interested by the long value, use `.hex_l`:\n\n```\n>>> c.hex_l\n'#0000ff'\n```\n\n### Modifying Color Objects\n\nAll of these properties are read/write, so let's add some red to this color:\n\n```\n>>> c.red = 1\n>>> c\n<Color magenta>\n```\n\nWe might want to de-saturate this color:\n\n```\n>>> c.saturation = 0.5\n>>> c\n<Color #bf40bf>\n```\n\nAnd of course, the string conversion will give the web representation which is human, or 3-digit, or 6-digit hex representation depending which is usable:\n\n```\n>>> f\"{c}\"\n'#bf40bf'\n\n>>> c.lightness = 1\n>>> f\"{c}\"\n'white'\n```\n\n### Ranges of Colors\n\nYou can get some color scale of variation between two `Color` objects quite easily. Here, is the color scale of the rainbow between red and blue:\n\n```\n>>> red = Color(\"red\")\n>>> blue = Color(\"blue\")\n>>> list(red.range_to(blue, 3))\n[<Color red>, <Color magenta>, <Color blue>]\n>>> list(red.range_to(blue, 5, longer=True))\n[<Color red>, <Color yellow>, <Color lime>, <Color cyan>, <Color blue>]\n```\n\nOr the different amount of gray between black and white:\n\n```\n>>> black = Color(\"black\")\n>>> white = Color(\"white\")\n>>> list(black.range_to(white, 6))\n[<Color black>, <Color #333>, <Color #666>, <Color #999>, <Color #ccc>, <Color white>]\n```\n\nIf you have to create graphical representation with color scale between red and green ('lime' color is full green):\n\n```\n>>> lime = Color(\"lime\")\n>>> list(red.range_to(lime, 5))\n[<Color red>, <Color #ff7f00>, <Color yellow>, <Color chartreuse>, <Color lime>]\n```\n\nNotice how naturally, the yellow is displayed in human format and in the middle of the scale. And that the quite unusual (but compatible) 'chartreuse' color specification has been used in place of the hexadecimal representation.\n\nYou can create your own scale with as many colors as you like:\n```\n>>> from colourings import color_scale\n>>> color_scale((Color(\"black\"), Color(\"orange\"), Color(\"blue\"), Color(\"white\")), 10)\n[Color(\"black\"), Color(\"#39221c\"), Color(\"#8e4d1c\"), Color(\"orange\"), Color(\"#ff003c\"), Color(\"#e100ff\"), Color(\"blue\"), Color(\"#bd71e3\"), Color(\"#e3c6d9\"), Color(\"white\")]\n```\n\n### Color Comparison\n\n#### Sane Default\n\nColor comparison is a vast subject. However, it might seem quite straightforward for you. `Color` uses a configurable default way of comparing color that might suit your needs:\n\n```\n>>> Color(\"red\") == Color(\"#f00\") == Color(\"blue\", hue=0)\nTrue\n```\n\nThe default comparison algorithm focuses only on the \"web\" representation which is equivalent to comparing the long hex representation (e.g. #FF0000) or to be more specific, it is equivalent to compare the amount of red, green, and blue composition of the RGB representation, each of these value being quantized to a 256 value scale.\n\nThis default comparison is a practical and convenient way to measure the actual color equivalence on your screen, or in your video card memory.\n\nBut this comparison wouldn't make the difference between a black red, and a black blue, which both are black:\n\n```\n>>> black_red = Color(\"red\", lightness=0)\n>>> black_blue = Color(\"blue\", lightness=0)\n\n>>> black_red == black_blue\nTrue\n```\n\n#### Customization\n\nBut, this is not the sole way to compare two colors. As I'm quite lazy, I'm providing you a way to customize it to your needs. Thus:\n\n```\n>>> from colourings import RGB_equivalence, HSL_equivalence\n>>> black_red = Color(\"red\", lightness=0, equality=HSL_equivalence)\n>>> black_blue = Color(\"blue\", lightness=0, equality=HSL_equivalence)\n\n>>> black_red == black_blue\nFalse\n```\n\nAs you might have already guessed, the sane default is `RGB_equivalence`, so:\n\n```\n>>> black_red = Color(\"red\", lightness=0, equality=RGB_equivalence)\n>>> black_blue = Color(\"blue\", lightness=0, equality=RGB_equivalence)\n\n>>> black_red == black_blue\nTrue\n```\n\nHere's how you could implement your unique comparison function:\n\n```\n>>> saturation_equivalence = lambda c1, c2: c1.saturation == c2.saturation\n>>> red = Color(\"red\", equality=saturation_equivalence)\n>>> blue = Color(\"blue\", equality=saturation_equivalence)\n>>> white = Color(\"white\", equality=saturation_equivalence)\n\n>>> red == blue\nTrue\n>>> white == red\nFalse\n```\n\nNote: When comparing 2 colors, only the equality function of the first color will be used. Thus:\n\n```\n>>> black_red = Color(\"red\", luminance=0, equality=RGB_equivalence)\n>>> black_blue = Color(\"blue\", luminance=0, equality=HSL_equivalence)\n\n>>> black_red == black_blue\nTrue\n```\nBut reverse operation is not equivalent !:\n\n```\n>>> black_blue == black_red\nFalse\n```\n\n### Equality to Non-`Color` Objects\nAs a side note, whatever your custom equality function is, it won't be used if you compare to anything else than a `Color` instance:\n\n```\n>>> red = Color(\"red\", equality=lambda c1, c2: True)\n>>> blue = Color(\"blue\", equality=lambda c1, c2: True)\n```\nNote that these instances would compare as equal to any other color:\n\n```\n>>> red == blue\nTrue\n```\n\nBut on another non-`Color` object:\n\n```\n>>> red == None\nFalse\n>>> red != None\nTrue\n```\n\nActually, `Color` instances will, politely enough, leave the other side of the equality have a chance to decide of the output, (by executing its own __eq__), so:\n\n```\n>>> class OtherColorImplem():\n...     def __init__(self, color):\n...         self.color = color\n...     def __eq__(self, other):\n...         return self.color == other.web\n\n>>> alien_red = OtherColorImplem(\"red\")\n>>> red == alien_red\nTrue\n>>> blue == alien_red\nFalse\n```\n\nAnd inequality (using __ne__) are also polite:\n\n```\n>>> class AnotherColorImplem(OtherColorImplem):\n...     def __ne__(self, other):\n...         return self.color != other.web\n\n>>> new_alien_red = AnotherColorImplem(\"red\")\n>>> red != new_alien_red\nFalse\n>>> blue != new_alien_red\nTrue\n```\n\n## Picking Arbitrary Color for a Python Object\n### Basic Usage\nSometimes, you just want to pick a color for an object in your application often to visually identify this object. Thus, the picked color should be the same for same objects, and different for different object:\n\n```\n>>> foo = object()\n>>> bar = object()\n\n>>> Color(pick_for=foo)\n<Color ...>\n>>> Color(pick_for=foo) == Color(pick_for=foo)\nTrue\n>>> Color(pick_for=foo) == Color(pick_for=bar)\nFalse\n```\n\nOf course, although there's a tiny probability that different strings yield the same color, most of the time, different inputs will produce different colors.\n\n#### Advanced Usage\nYou can customize your color picking algorithm by providing a picker. A picker is a callable that takes an object, and returns something that can be instantiated as a color by Color:\n\n```\n>>> my_picker = lambda obj: \"red\" if isinstance(obj, int) else \"blue\"\n>>> Color(pick_for=3, picker=my_picker, pick_key=None)\n<Color red>\n>>> Color(pick_for=\"foo\", picker=my_picker, pick_key=None)\n<Color blue>\n```\n\nYou might want to use a particular picker, but enforce how the picker will identify two object as the same (or not). So there's a pick_key attribute that is provided and defaults as equivalent of hash method and if hash is not supported by your object, it'll default to the str of your object salted with the class name.\n\nThus:\n\n```\n>>> class MyObj(str): pass\n>>> my_obj_color = Color(pick_for=MyObj(\"foo\"))\n>>> my_str_color = Color(pick_for=\"foo\")\n>>> my_obj_color == my_str_color\nFalse\n```\n\nPlease make sure your object is hashable or \"stringable\" before using the RGB_color_picker picking mechanism or provide another color picker. Nearly all python object are hashable by default so this shouldn't be an issue (e.g. instances of object and subclasses are hashable).\n\nNeither hash nor str are perfect solution. So feel free to use pick_key at Color instantiation time to set your way to identify objects, for instance:\n\n```\n>>> a = object()\n>>> b = object()\n>>> Color(pick_for=a, pick_key=id) == Color(pick_for=b, pick_key=id)\nFalse\n```\n\nWhen choosing a pick key, you should closely consider if you want your color to be consistent between runs (this is NOT the case with the last example), or consistent with the content of your object if it is a mutable object.\n\nDefault value of pick_key and picker ensures that the same color will be attributed to same object between different run on different computer for most python object.\n\n### Color Factory\nAs you might have noticed, there are few attributes that you might want to see attached to all of your colors as equality for equality comparison support, or picker, pick_key to configure your object color picker.\n\nYou can create a customized Color factory thanks to the make_color_factory:\n\n```\n>>> from colourings import make_color_factory, HSL_equivalence, RGB_color_picker\n\n>>> get_color = make_color_factory(\n...    equality=HSL_equivalence,\n...    picker=RGB_color_picker,\n...    pick_key=str,\n... )\n```\n\nAll color created thanks to CustomColor class instead of the default one would get the specified attributes by default:\n\n```\n>>> black_red = get_color(\"red\", luminance=0)\n>>> black_blue = get_color(\"blue\", luminance=0)\nOf course, these are always instances of Color class:\n\n>>> isinstance(black_red, Color)\nTrue\nEquality was changed from normal defaults, so:\n\n>>> black_red == black_blue\nFalse\n```\n\nThis because the default equivalence of Color was set to HSL_equivalence.\n\n## Contributing\nAny suggestion or issue is welcome. Push request are very welcome, please check out the guidelines.\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause license\n        Copyright (c) 2025, Daniel Stoops\n        All rights reserved.\n        \n        Redistribution and use in source and binary forms, with or without\n        modification, are permitted provided that the following conditions are met:\n        \n          1. Redistributions of source code must retain the above copyright notice,\n             this list of conditions and the following disclaimer.\n          2. Redistributions in binary form must reproduce the above copyright\n             notice, this list of conditions and the following disclaimer in the\n             documentation and/or other materials provided with the distribution.\n          3. Neither the name of the copyright holder nor the names of its\n             contributors may be used to endorse or promote products derived from\n             this software without specific prior written permission.\n        \n        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n        ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR\n        ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n        LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n        OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH\n        DAMAGE.\n        ",
    "summary": "Colouring in Python",
    "version": "0.2.3",
    "project_urls": {
        "Repository": "https://github.com/Stoops-ML/colourings"
    },
    "split_keywords": [
        "color",
        " colour"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b52a587283b68c5ab450e2ce9d38f22d8b2577d20d8598717c2162587236164b",
                "md5": "f7162e5012c176234fd88812ee9cb879",
                "sha256": "940e8e0bd087636215001333680c3802f99e546d06e9553e7838d9ab5468a154"
            },
            "downloads": -1,
            "filename": "colourings-0.2.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f7162e5012c176234fd88812ee9cb879",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 17748,
            "upload_time": "2025-02-26T09:06:09",
            "upload_time_iso_8601": "2025-02-26T09:06:09.306291Z",
            "url": "https://files.pythonhosted.org/packages/b5/2a/587283b68c5ab450e2ce9d38f22d8b2577d20d8598717c2162587236164b/colourings-0.2.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "44e80418ef58e441a2f8cc05da8f5be9399e17623d6c2aa0b37e656e4912bd07",
                "md5": "662de8221cb74fd8ce2050d997faea21",
                "sha256": "1cc8d3a2763f07ccdd993ad959bf0adbd3282363ee2e63a7c87131a2213ee3fd"
            },
            "downloads": -1,
            "filename": "colourings-0.2.3.tar.gz",
            "has_sig": false,
            "md5_digest": "662de8221cb74fd8ce2050d997faea21",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 27647,
            "upload_time": "2025-02-26T09:06:12",
            "upload_time_iso_8601": "2025-02-26T09:06:12.810746Z",
            "url": "https://files.pythonhosted.org/packages/44/e8/0418ef58e441a2f8cc05da8f5be9399e17623d6c2aa0b37e656e4912bd07/colourings-0.2.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-26 09:06:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Stoops-ML",
    "github_project": "colourings",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "colourings"
}
        
Elapsed time: 0.48116s