Python による画像生成で目指せ、絶対色彩感覚

### 何がやりたいのか

[こちら](https://qiita.com/nu_no_kehai/items/7aa85c735d7ae2878629)と[こちら](https://qiita.com/nu_no_kehai/items/6b1d13bb4e3ade743bc2)で文字入りの壁紙を作る方法を検討していたのですが、もっといろんな色を試したい、が……
色名だけ見てもどんな色か分かりません。勘で２色選んで画像を作成して試すのも限度がある。
ということで、色見本の自動生成にチャレンジです。プログラム本文は下記。

結果……
１回目。
![color_sample_01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351420/0ccfe867-ffa0-0f6f-9c07-ce74905fcfda.png)

２回目。
![color_sample_02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351420/f35d767d-2e91-c1c8-41ef-9c919e976553.png)

３回目。
![color_sample_03.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/351420/d60605bc-ef79-809a-d792-b17b78caf94b.png)


こんな感じになりました。bgとtxtの色が被ることがあるのが要検討ですが、遊びならまあ、スルーでいいでしょう。
こうして見ると、センスの良い色の組み合わせってむしろ少数派だと分かりますね。
色名と実際の色調が結びつくのがこういうプログラムを作る第一の効能かもしれません。
世の中のデザイナーさん、いかがでしょうか。

### その他
* カラーコードの一覧は以下のサイトを参考にさせていただきました。

https://itsakura.com/html-color-codes

* このページの記載内容について、改変して公開などはご自由に。

### プログラム本文

```python:run.py
#import pyautogui as pag
from PIL import Image, ImageFont, ImageDraw
import cv2
import numpy as np
from color import *

show_color_name() #利用可能な色名一覧を表示

#screen = pag.size()

image_width = 3000
image_height = 1800
print("image width = ",image_width)
print("image height = ",image_height)
div_x = 15
div_y = 9

box_x = int(image_width/div_x)
box_y = int(image_height/div_y)

out_file_name = "color_sample.png"
color_table_txt=color_table_random(div_x*div_y)
color_table_bg=color_table_random(div_x*div_y)

string_txt =  "あ"
rate_font_size = 0.5
rate_pos_txt = [0.25, 0.25]

string_txt_small = ""
rate_font_size_small = 0.10
rate_pos_txt_small = [0.05, 0.75]

font_dir = '/Users/(your username)/Library/Fonts/'
if ( font_dir == "" ):
    font_dir = "/System/Library/Fonts/"
font_file = 'sawarabi-gothic/sawarabi-gothic-medium.ttf'
font_path = font_dir + font_file

font_size = int(rate_font_size*box_x)
font = ImageFont.truetype(font_path, font_size)

pos_txt = ( int(rate_pos_txt[0]*box_x),int(rate_pos_txt[1]*box_y) )

font_size_small = int(rate_font_size_small*box_x)
pos_txt_small = ( int(rate_pos_txt_small[0]*box_x),int(rate_pos_txt_small[1]*box_y) )
font_small = ImageFont.truetype(font_path, font_size_small)

img = np.zeros((image_height, image_width, 3), np.uint8)

n = -1
for ix in range(0,div_x):
    nx = box_x*ix
    for iy in range(0,div_y):
        ny = box_y*iy
        n = n + 1

        rgb_bg=color_table_bg[n].rgb
        for i in range(0,3):
            img[ny:ny+box_y, nx:nx+box_x, i] = rgb_bg[2-i]

        img = Image.fromarray(img) # cv2(NumPy)型の画像をPIL型に変換
        draw = ImageDraw.Draw(img)

        rgb_txt=color_table_txt[n].rgb
        color_txt =(rgb_txt[2],rgb_txt[1],rgb_txt[0],0)
        pos_txt_now = (nx+pos_txt[0],ny+pos_txt[1])
        draw.text(pos_txt_now, string_txt, font=font, fill=color_txt)

        if color_table_bg[n].name == "Black":
                    color_txt_small=(255,255,255,0)
        else:
                    color_txt_small=(0,0,0,0)
        string_txt_small = "txt: "+color_table_txt[n].name
        string_txt_small += "\nbg: "+color_table_bg[n].name
        pos_txt_small_now = (nx+pos_txt_small[0],ny+pos_txt_small[1])
        draw.text(pos_txt_small_now, string_txt_small, font=font_small, fill=color_txt_small)

        img = np.array(img) # PIL型の画像をcv2(NumPy)型に変換

cv2.imwrite(out_file_name, img)
```

```python:color.py
COLORS = {
    "IndianRed": (205,92,92),
    "LightCoral": (240,128,128),
    "Salmon": (250,128,114),
    "DarkSalmon": (233,150,122),
    "LightSalmon": (255,160,122),
    "Crimson": (220,20,60),
    "Red": (255,0,0),
    "FireBrick": (178,34,34),
    "DarkRed": (139,0,0),
    "Pink": (255,192,203),
    "LightPink": (255,182,193),
    "HotPink": (255,105,180),
    "DeepPink": (255,20,147),
    "MediumVioletRed": (199,21,133),
    "PaleVioletRed": (219,112,147),
    "Coral": (255,127,80),
    "Tomato": (255,99,71),
    "OrangeRed": (255,69,0),
    "DarkOrange": (255,140,0),
    "Orange": (255,165,0),
    "Gold": (255,215,0),
    "Yellow": (255,255,0),
    "LightYellow": (255,255,224),
    "LemonChiffon": (255,250,205),
    "LightGoldenrodYellow": (250,250,210),
    "PapayaWhip": (255,239,213),
    "Moccasin": (255,228,181),
    "PeachPuff": (255,218,185),
    "PaleGoldenrod": (238,232,170),
    "Khaki" : (240,230,140),
    "DarkKhaki": (189,183,107),
    "Lavender": (230,230,250),
    "Thistle": (216,191,216),
    "Plum": (221,160,221),
    "Violet" : (238,130,238),
    "Orchid": (218,112,214),
    "Fuchsia": (255,0,255),
    "Magenta": (255,0,255),
    "MediumOrchid": (186,85,211),
    "MediumPurple": (147,112,219),
    "Amethyst": (153,102,204),
    "BlueViolet": (138,43,226),
    "DarkViolet": (148,0,211),
    "DarkOrchid": (153,50,204),
    "DarkMagenta": (139,0,139),
    "Purple": (128,0,128),
    "Indigo": (75,0,130),
    "SlateBlue": (106,90,205),
    "DarkSlateBlue": (72,61,139),
    "GreenYellow": (173,255,47),
    "Chartreuse": (127,255,0),
    "LawnGreen": (124,252,0),
    "Lime": (0,255,0),
    "LimeGreen": (50,205,50),
    "PaleGreen": (152,251,152),
    "LightGreen": (144,238,144),
    "MediumSpringGreen": (0,250,154),
    "SpringGreen": (0,255,127),
    "MediumSeaGreen": (60,179,113),
    "SeaGreen": (46,139,87),
    "ForestGreen": (34,139,34),
    "Green": (0,128,0),
    "DarkGreen": (0,100,0),
    "YellowGreen": (154,205,50),
    "OliveDrab": (107,142,35),
    "Olive": (128,128,0),
    "DarkOliveGreen": (85,107,47),
    "MediumAquamarine": (102,205,170),
    "DarkSeaGreen": (143,188,143),
    "LightSeaGreen": (32,178,170),
    "DarkCyan": (0,139,139),
    "Teal": (0,128,128),
    "Aqua": (0,255,255),
    "Cyan" : (0,255,255),
    "LightCyan": (224,255,255),
    "PaleTurquoise": (175,238,238),
    "Aquamarine": (127,255,212),
    "Turquoise": (64,224,208),
    "MediumTurquoise": (72,209,204),
    "DarkTurquoise": (0,206,209),
    "CadetBlue": (95,158,160),
    "SteelBlue": (70,130,180),
    "LightSteelBlue": (176,196,222),
    "PowderBlue": (176,224,230),
    "LightBlue": (173,216,230),
    "SkyBlue": (135,206,235),
    "LightSkyBlue": (135,206,250),
    "DeepSkyBlue": (0,191,255),
    "DodgerBlue": (30,144,255),
    "CornflowerBlue": (100,149,237),
    "MediumSlateBlue": (123,104,238),
    "RoyalBlue": (65,105,225),
    "Blue": (0,0,255),
    "MediumBlue": (0,0,205),
    "DarkBlue": (0,0,139),
    "Navy": (0,0,128),
    "MidnightBlue": (25,25,112),
    "Cornsilk": (255,248,220),
    "BlanchedAlmond": (255,235,205),
    "BlanchedAlmond": (255,228,196),
    "NavajoWhite": (255,222,173),
    "Wheat": (245,222,179),
    "BurlyWood": (222,184,135),
    "Tan": (210,180,140),
    "RosyBrown": (188,143,143),
    "SandyBrown": (244,164,96),
    "Goldenrod": (218,165,32),
    "DarkGoldenrod": (184,134,11),
    "Peru": (205,133,63),
    "Chocolate": (210,105,30),
    "SaddleBrown": (139,69,19),
    "Sienna": (160,82,45),
    "Brown": (165,42,42),
    "Maroon": (128,0,0),
    "White": (255,255,255),
    "Snow": (255,250,250),
    "Honeydew": (240,255,240),
    "MintCream": (245,255,250),
    "Azure": (240,255,255),
    "AliceBlue": (240,248,255),
    "GhostWhite": (248,248,255),
    "WhiteSmoke": (245,245,245),
    "Seashell": (255,245,238),
    "Beige": (245,245,220),
    "OldLace": (253,245,230),
    "FloralWhite": (255,250,240),
    "Ivory": (255,255,240),
    "AntiqueWhite": (250,235,215),
    "Linen": (250,240,230),
    "LavenderBlush": (255,240,245),
    "MistyRose": (255,228,225),
    "Gainsboro": (220,220,220),
    "LightGrey": (211,211,211),
    "Silver": (192,192,192),
    "DarkGray": (169,169,169),
    "Gray": (128,128,128),
    "DimGray": (105,105,105),
    "LightSlateGray": (119,136,153),
    "SlateGray": (112,128,144),
    "DarkSlateGray": (47,79,79),
    "Black": (0,0,0),
    "紺色": (35,59,108),
    "藍色": (22,94,131),
    "桔梗色": (106,76,156),
    "江戸紫": (116,83,153),
    "鶯色": (145,141,64),
    "灰汁色": (188,176,156),
    "亜麻色": (214,198,175),
    "山吹色": (248,180,0),
    "小豆色": (152,81,75),
    "えんじ色": (179,66,74),
    "茜色": (177,53,70),
}


class Color:
  def __init__(self, name, rgb):
    self.name = name
    self.r, self.g, self.b = self.rgb = rgb
    self.code = f'#{self.r:02x}{self.g:02x}{self.b:02x}'


def color_table_full():
    return [Color(name, rgb) for name, rgb in COLORS.items()]


def find_color(name):
    return Color(name, COLORS[name]) if name in COLORS else None


def show_color_names():
    print(*COLORS)


def color_table_random(num_color):

    import random

    color_table = color_table_full()

    result = []

    for i in range(0,num_color):
        result.append(color_table[random.randrange(len(color_table))])

    return result
```