[PYTHON] Manipulate colors with pip install color

Introduction

Since plotly and pyqtgraph do not have (or few?) Color palettes, it is more difficult to specify colors than matplotlib. This time, I will introduce a module that allows you to easily create color gradations.

environment

Mac OS Python 3.8.5 colour 0.1.5

pip install colour

Details

Create

All will be red.

from colour import Color

Color('red')
Color(red=1)
Color('#f00')
Color('#ff0000')
Color(rgb=(1, 0, 0))  # 0~1
Color(hsl=(0, 1, 0.5))  # 0~1
Color(Color('red'))

Get value

from colour import Color

c = Color('red')  # <class 'colour.Color'>

Gets the value of c.

RGB

rgb = c.get_rgb()  # (1.0, 0.0, 0.0), tuple
rgb_hex = c.get_hex()  # #f00, str
rgb_hex_long = c.get_hex_l()  # #ff0000, str

HSL

hsl = c.get_hsl()  #hsl color space(0.0, 1.0, 0.5), tuple
hue = c.get_hue()  #Hue 0.0, float
saturation = c.get_saturation()  #Saturation 1.0, float
luminance = c.get_luminance()  #Luminance 0.5, float

monochromatic

red = c.get_red()  # 1.0, float
blue = c.get_blue()  # 0.0, float
green = c.get_green()  # 0.0, float

Value change

c = Color('red')  # c = red

Rewrite the value of c

c.set_blue(1)  # c = magenta, set_red(), set_green()There is also
c.set_saturation(0.5)  #Change saturation, c = #bf40bf, 0~Between 1
c.set_luminance(0.2)  #Change brightness, c = #4c194c, 0~Between 1
#The value is overwritten
c.set_rgb((1, 0, 0))
c.set_hsl((0, 1, 0.5))

Judgment

Color('red') == Color('blue')
False
Color('red') == Color('red')
True
Color('red') != Color('blue')
True
Color('red') != Color('red')
False

Gradation

Specify the start color, end color, and the number of divisions

# red -Between blue
red = Color('red')
blue = Color('blue')
#5 divisions[<Color red>, <Color yellow>, <Color lime>, <Color cyan>, <Color blue>]
red_blue = list(red.range_to(blue, 5))

# black -Between white
black = Color('black')
white = Color('white')
#6 divisions[<Color black>, <Color #333>, <Color #666>, <Color #999>, <Color #ccc>, <Color white>]
black_white = list(black.range_to(white, 6))

Example of use

Since matplotlib and seaborn can specify many colors from the beginning, you may not use them much. I think it can be used for plotly.

スクリーンショット 2021-01-13 22.01.20.png
from colour import Color
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

red = Color('red')
blue = Color('blue')
red_blue = list(red.range_to(blue, 100))

for num, color in enumerate(red_blue, 1):
    ax.plot([i for i in range(10)],
            [i * num for i in range(10)],
            c=color.get_hex_l())

plt.show()

reference

colour · PyPI

Recommended Posts

Manipulate colors with pip install color
Error with pip install
Install scikit.learn with pip
Install github repository with pip
Install Python 2.7.9 and Python 3.4.x with pip.
Permission denied with pip install pyopenssl
How to install mysql-connector with pip3
[Python Windows] pip install with Python version
SSL certificate related with pip install? Error
I want to pip install with PythonAnywhere
Install by specifying the version with pip
I can't install the package with pip.
Let's install box2d-py with Windows 10 environment pip
sudo pip install
I can't install Dask with pip on Ubuntu
Avoiding "'ascii' codec can't decode" with pip install
Install pip and pandas with Ubuntu or VScode
Align Matplotlib graph colors with similar colors (color map)
Steps to install your own library with pip
Install pip on Mavericks
PIL installation with pip
Install pip, pyenv, BeautifulSoup4
pip install ○○○ error Command errored out with exit status 1:
Installing packages with pip3
How to install pip
Use pip with MSYS2
Install Voluptuous with Python 2.5
Install the pip command
Install torch-scatter with PyTorch 1.7
Install python with pyenv
A memorandum when an error occurs with pip install
Until you install TensorFlow-GPU with pip in Windows environment
Install pip / pip3 on Ubuntu
When pip install fails
Install tweepy with pip and use it for API 1.1
[Road to intermediate Python] Install packages in bulk with pip
Until you can install your own Python library with pip
Can't find the package you installed with pip install --user?
Manually fix pip install not possible with Unicode Decode Eroor
Steps to install a Git cloned package locally with pip
Install Jupiter Notebook on Windows with pip in proxy environment
I have libncurses but get angry with pip install readline
Bulk update with pip (with confirmation)
Install Python environment with Anaconda
Install Keras (used with Anaconda)
Use pip with Jupyter Notebook
Specify version with conda install
Pip install in proxy environment
Install external libraries with Python
Manipulate multiple proxies with Squid
Pip install the GitHub repository
Manipulate various databases with Python
Gradient color selection with matplotlib
Dealing with PermissionError [Error 1] for pip install -U pip on macOS Sierra
What to do if you can't install pyaudio with pip #Python
I get an error when trying to install maec 4.0.1.0 with pip
Install packages that need to be compiled with Python3 with pip [Windows]
Read this if you get SSL related errors with pip install! !! !!
Install pip in Serverless Framework and AWS Lambda with Python environment
[TensorFlow] If you want to run TensorBoard, install it with pip