| 1 | [](https://www.python.org/) |
| 2 | [](https://www.djangoproject.com/) |
| 3 | |
| 4 | [](https://pypi.org/project/django-colorfield/) |
| 5 | [](https://pepy.tech/project/django-colorfield) |
| 6 | [](https://github.com/fabiocaccamo/django-colorfield/stargazers) |
| 7 | [](https://github.com/fabiocaccamo/django-colorfield/blob/main/LICENSE.txt) |
| 8 | |
| 9 | [](https://results.pre-commit.ci/latest/github/fabiocaccamo/django-colorfield/main) |
| 10 | [](https://github.com/fabiocaccamo/django-colorfield) |
| 11 | [](https://codecov.io/gh/fabiocaccamo/django-colorfield) |
| 12 | [](https://www.codacy.com/app/fabiocaccamo/django-colorfield) |
| 13 | [](https://codeclimate.com/github/fabiocaccamo/django-colorfield/) |
| 14 | [](https://github.com/psf/black) |
| 15 | [](https://github.com/astral-sh/ruff) |
| 16 | |
| 17 | # django-colorfield |
| 18 | simple color field for your models with a nice color-picker in the admin-interface. |
| 19 | |
| 20 |  |
| 21 |  |
| 22 | |
| 23 | --- |
| 24 | |
| 25 | ## Installation |
| 26 | - Run `pip install django-colorfield` |
| 27 | - Add `colorfield` to `settings.INSTALLED_APPS` |
| 28 | - Run `python manage.py collectstatic` |
| 29 | - Restart your application server |
| 30 | |
| 31 | --- |
| 32 | |
| 33 | ## Usage |
| 34 | |
| 35 | ### Settings |
| 36 | This package doesn't need any setting. |
| 37 | |
| 38 | ### Models |
| 39 | Just add color field(s) to your models like this: |
| 40 | |
| 41 | ```python |
| 42 | from colorfield.fields import ColorField |
| 43 | from django.db import models |
| 44 | |
| 45 | class MyModel(models.Model): |
| 46 | color = ColorField(default='#FF0000') |
| 47 | ``` |
| 48 | |
| 49 | ### Field Options |
| 50 | These are the supported custom options: [`format`](#format), [`image_field`](#image_field), [`samples`](#samples) |
| 51 | |
| 52 | #### format |
| 53 | |
| 54 | The following formats are supported: `hex` *(default)*, `hexa`, `rgb`, `rgba`. |
| 55 | |
| 56 | ```python |
| 57 | from colorfield.fields import ColorField |
| 58 | from django.db import models |
| 59 | |
| 60 | class MyModel(models.Model): |
| 61 | color = ColorField(format="hexa") |
| 62 | ``` |
| 63 | |
| 64 | #### image_field |
| 65 | |
| 66 | It is possible to auto-populate the field value getting the color from an image using the `image_field` option. |
| 67 | |
| 68 | The color will be calculated from the **top-left pixel** color of the image each time the model instance is saved. |
| 69 | |
| 70 | ```python |
| 71 | from colorfield.fields import ColorField |
| 72 | from django.db import models |
| 73 | |
| 74 | class MyModel(models.Model): |
| 75 | image = models.ImageField(upload_to="images") |
| 76 | color = ColorField(image_field="image") |
| 77 | ``` |
| 78 | |
| 79 | #### samples |
| 80 | |
| 81 | It is possible to provide a palette of colors to choose from to the widget using the `samples` option. |
| 82 | |
| 83 | This option **is not restrictive** (on the contrary of `choices` option), it is also possible to choose another color from the spectrum. |
| 84 | |
| 85 |  |
| 86 | |
| 87 | ```python |
| 88 | from colorfield.fields import ColorField |
| 89 | from django.db import models |
| 90 | |
| 91 | class MyModel(models.Model): |
| 92 | |
| 93 | COLOR_PALETTE = [ |
| 94 | ("#FFFFFF", "white", ), |
| 95 | ("#000000", "black", ), |
| 96 | ] |
| 97 | |
| 98 | # not restrictive, allows the selection of another color from the spectrum. |
| 99 | color = ColorField(samples=COLOR_PALETTE) |
| 100 | |
| 101 | # restrictive, it is mandatory to choose a color from the palette |
| 102 | color = ColorField(choices=COLOR_PALETTE) |
| 103 | ``` |
| 104 | |
| 105 | ### Admin |
| 106 | The admin will kindly provide a simple [color picker](http://jscolor.com/) for all color fields. :) |
| 107 | |
| 108 | --- |
| 109 | |
| 110 | ## Testing |
| 111 | ```bash |
| 112 | # clone repository |
| 113 | git clone https://github.com/fabiocaccamo/django-colorfield.git && cd django-colorfield |
| 114 | |
| 115 | # create virtualenv and activate it |
| 116 | python -m venv venv && . venv/bin/activate |
| 117 | |
| 118 | # upgrade pip |
| 119 | python -m pip install --upgrade pip |
| 120 | |
| 121 | # install requirements |
| 122 | pip install -r requirements.txt -r requirements-test.txt |
| 123 | |
| 124 | # install pre-commit to run formatters and linters |
| 125 | pre-commit install --install-hooks |
| 126 | |
| 127 | # run tests |
| 128 | tox |
| 129 | # or |
| 130 | python runtests.py |
| 131 | # or |
| 132 | python -m django test --settings "tests.settings" |
| 133 | ``` |
| 134 | --- |
| 135 | |
| 136 | ## Credits |
| 137 | Originally developed by [Jared Forsyth](https://github.com/jaredly) |
| 138 | |
| 139 | --- |
| 140 | |
| 141 | ## License |
| 142 | Released under [MIT License](LICENSE.txt). |
| 143 | |
| 144 | --- |
| 145 | |
| 146 | ## Supporting |
| 147 | |
| 148 | - :star: Star this project on [GitHub](https://github.com/fabiocaccamo/django-colorfield) |
| 149 | - :octocat: Follow me on [GitHub](https://github.com/fabiocaccamo) |
| 150 | - :blue_heart: Follow me on [Twitter](https://twitter.com/fabiocaccamo) |
| 151 | - :moneybag: Sponsor me on [Github](https://github.com/sponsors/fabiocaccamo) |
| 152 | |
| 153 | ## See also |
| 154 | |
| 155 | - [`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. ๐ง โก |
| 156 | |
| 157 | - [`django-extra-settings`](https://github.com/fabiocaccamo/django-extra-settings) - config and manage typed extra settings using just the django admin. โ๏ธ |
| 158 | |
| 159 | - [`django-maintenance-mode`](https://github.com/fabiocaccamo/django-maintenance-mode) - shows a 503 error page when maintenance-mode is on. ๐ง ๐ ๏ธ |
| 160 | |
| 161 | - [`django-redirects`](https://github.com/fabiocaccamo/django-redirects) - redirects with full control. โช๏ธ |
| 162 | |
| 163 | - [`django-treenode`](https://github.com/fabiocaccamo/django-treenode) - probably the best abstract model / admin for your tree based stuff. ๐ณ |
| 164 | |
| 165 | - [`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. ๐ |
| 166 | |
| 167 | - [`python-codicefiscale`](https://github.com/fabiocaccamo/python-codicefiscale) - encode/decode Italian fiscal codes - codifica/decodifica del Codice Fiscale. ๐ฎ๐น ๐ณ |
| 168 | |
| 169 | - [`python-fontbro`](https://github.com/fabiocaccamo/python-fontbro) - friendly font operations. ๐งข |
| 170 | |
| 171 | - [`python-fsutil`](https://github.com/fabiocaccamo/python-fsutil) - file-system utilities for lazy devs. ๐งโโ๏ธ |