1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
[](https://www.python.org/)
[](https://www.djangoproject.com/)
[](https://pypi.org/project/django-colorfield/)
[](https://pepy.tech/project/django-colorfield)
[](https://github.com/fabiocaccamo/django-colorfield/stargazers)
[](https://github.com/fabiocaccamo/django-colorfield/blob/main/LICENSE.txt)
[](https://results.pre-commit.ci/latest/github/fabiocaccamo/django-colorfield/main)
[](https://github.com/fabiocaccamo/django-colorfield)
[](https://codecov.io/gh/fabiocaccamo/django-colorfield)
[](https://www.codacy.com/app/fabiocaccamo/django-colorfield)
[](https://codeclimate.com/github/fabiocaccamo/django-colorfield/)
[](https://github.com/psf/black)
[](https://github.com/astral-sh/ruff)
# django-colorfield
simple color field for your models with a nice color-picker in the admin-interface.


---
## Installation
- Run `pip install django-colorfield`
- Add `colorfield` to `settings.INSTALLED_APPS`
- Run `python manage.py collectstatic`
- Restart your application server
---
## Usage
### Settings
This package doesn't need any setting.
### Models
Just add color field(s) to your models like this:
```python
from colorfield.fields import ColorField
from django.db import models
class MyModel(models.Model):
color = ColorField(default='#FF0000')
```
### Field Options
These are the supported custom options: [`format`](#format), [`image_field`](#image_field), [`samples`](#samples)
#### format
The following formats are supported: `hex` *(default)*, `hexa`, `rgb`, `rgba`.
```python
from colorfield.fields import ColorField
from django.db import models
class MyModel(models.Model):
color = ColorField(format="hexa")
```
#### image_field
It is possible to auto-populate the field value getting the color from an image using the `image_field` option.
The color will be calculated from the **top-left pixel** color of the image each time the model instance is saved.
```python
from colorfield.fields import ColorField
from django.db import models
class MyModel(models.Model):
image = models.ImageField(upload_to="images")
color = ColorField(image_field="image")
```
#### samples
It is possible to provide a palette of colors to choose from to the widget using the `samples` option.
This option **is not restrictive** (on the contrary of `choices` option), it is also possible to choose another color from the spectrum.

```python
from colorfield.fields import ColorField
from django.db import models
class MyModel(models.Model):
COLOR_PALETTE = [
("#FFFFFF", "white", ),
("#000000", "black", ),
]
# not restrictive, allows the selection of another color from the spectrum.
color = ColorField(samples=COLOR_PALETTE)
# restrictive, it is mandatory to choose a color from the palette
color = ColorField(choices=COLOR_PALETTE)
```
### Admin
The admin will kindly provide a simple [color picker](http://jscolor.com/) for all color fields. :)
---
## Testing
```bash
# clone repository
git clone https://github.com/fabiocaccamo/django-colorfield.git && cd django-colorfield
# create virtualenv and activate it
python -m venv venv && . venv/bin/activate
# upgrade pip
python -m pip install --upgrade pip
# install requirements
pip install -r requirements.txt -r requirements-test.txt
# install pre-commit to run formatters and linters
pre-commit install --install-hooks
# run tests
tox
# or
python runtests.py
# or
python -m django test --settings "tests.settings"
```
---
## Credits
Originally developed by [Jared Forsyth](https://github.com/jaredly)
---
## License
Released under [MIT License](LICENSE.txt).
---
## Supporting
- :star: Star this project on [GitHub](https://github.com/fabiocaccamo/django-colorfield)
- :octocat: Follow me on [GitHub](https://github.com/fabiocaccamo)
- :blue_heart: Follow me on [Twitter](https://twitter.com/fabiocaccamo)
- :moneybag: Sponsor me on [Github](https://github.com/sponsors/fabiocaccamo)
## See also
- [`django-admin-interface`](https://github.com/fabiocaccamo/django-admin-interface) - the default admin interface made customizable by the admin itself. popup windows replaced by modals. ๐ง โก
- [`django-extra-settings`](https://github.com/fabiocaccamo/django-extra-settings) - config and manage typed extra settings using just the django admin. โ๏ธ
- [`django-maintenance-mode`](https://github.com/fabiocaccamo/django-maintenance-mode) - shows a 503 error page when maintenance-mode is on. ๐ง ๐ ๏ธ
- [`django-redirects`](https://github.com/fabiocaccamo/django-redirects) - redirects with full control. โช๏ธ
- [`django-treenode`](https://github.com/fabiocaccamo/django-treenode) - probably the best abstract model / admin for your tree based stuff. ๐ณ
- [`python-benedict`](https://github.com/fabiocaccamo/python-benedict) - dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, json, pickle, plist, query-string, toml, xml, yaml) and many utilities. ๐
- [`python-codicefiscale`](https://github.com/fabiocaccamo/python-codicefiscale) - encode/decode Italian fiscal codes - codifica/decodifica del Codice Fiscale. ๐ฎ๐น ๐ณ
- [`python-fontbro`](https://github.com/fabiocaccamo/python-fontbro) - friendly font operations. ๐งข
- [`python-fsutil`](https://github.com/fabiocaccamo/python-fsutil) - file-system utilities for lazy devs. ๐งโโ๏ธ
|