File size: 7,170 Bytes
6a0e8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os \n",
    "from glob import glob \n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from PIL import Image, ImageColor\n",
    "import extcolors\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import torch\n",
    "\n",
    "import dnnlib \n",
    "import legacy\n",
    "\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "images_textiles = glob('/Users/ludovicaschaerf/Desktop/TextAIles/TextileGAN/Original Textiles/*')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### LAWS\n",
    "\n",
    "1. primary colours on small surfaces and secondary or tertiary colors on large backgrounds\n",
    "2. primary in upper portions and sec/third in lower portions of objects\n",
    "3. primaries of equal intensities harmonize, secondaries harmonized by opposite primary in equal intensity, tertiary by remaining secondary\n",
    "4. a full colors contrasted by a lower tone color should have the latter in larger proportion\n",
    "5. when a primary has a hue (second coloration) of another primary, the secondary must have the hue of the third primary\n",
    "6. blue in concave surfaces, yellow in convex, red in undersites\n",
    "7. if too much of a color, the other colors should have the hue version without that color\n",
    "8. all three primaries should be present\n",
    "9. ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Test 1\n",
    "\n",
    "primary - secondary - tertiary "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_color_rank(hue, saturation, value):\n",
    "    if value < 5:\n",
    "        color = 'Black'\n",
    "        rank = 'None'\n",
    "    elif saturation < 3:\n",
    "        color = 'White'\n",
    "        rank = 'None'\n",
    "    elif saturation < 15:\n",
    "        color = 'Gray'\n",
    "        rank = 'None'\n",
    "    elif hue == 0:\n",
    "        color = 'Gray'\n",
    "        rank = 'None'\n",
    "        \n",
    "    elif hue >= 330 or hue <= 15:\n",
    "        color = 'Red'\n",
    "        rank = 'Primary'\n",
    "    elif hue > 15 and hue < 25:\n",
    "        color = 'Red Orange'\n",
    "        rank = 'Tertiary'\n",
    "    elif hue >= 25 and hue <= 40:\n",
    "        color = 'Orange'\n",
    "        rank = 'Secondary'\n",
    "    elif hue > 40 and hue < 50:\n",
    "        color = 'Orange Yellow'\n",
    "        rank = 'Tertiary'\n",
    "    elif hue >= 50 and hue <= 85:\n",
    "        color = 'Yellow'\n",
    "        rank = 'Primary'\n",
    "    elif hue > 85 and hue < 95:\n",
    "        color = 'Yellow Green'\n",
    "        rank = 'Tertiary'\n",
    "    elif hue >= 95 and hue <= 145:\n",
    "        color = 'Green'\n",
    "        rank = 'Secondary'\n",
    "    elif hue >= 145 and hue < 180:\n",
    "        color = 'Green Blue'\n",
    "        rank = 'Tertiary'\n",
    "    elif hue >= 180 and hue <= 245:\n",
    "        color = 'Blue'\n",
    "        rank = 'Primary'\n",
    "    elif hue > 245 and hue < 265:\n",
    "        color = 'Blue Violet'\n",
    "        rank = 'Tertiary'\n",
    "    elif hue >= 265 and hue <= 290:\n",
    "        color = 'Violet'\n",
    "        rank = 'Secondary'\n",
    "    elif hue > 290 and hue < 330:\n",
    "        color = 'Violet Red'\n",
    "        rank = 'Tertiary'\n",
    "    return color, rank"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rgb2hsv(r, g, b):\n",
    "    # Normalize R, G, B values\n",
    "    r, g, b = r / 255.0, g / 255.0, b / 255.0\n",
    "    \n",
    "    # h, s, v = hue, saturation, value\n",
    "    max_rgb = max(r, g, b)    \n",
    "    min_rgb = min(r, g, b)   \n",
    "    difference = max_rgb-min_rgb \n",
    "    \n",
    "    # if max_rgb and max_rgb are equal then h = 0\n",
    "    if max_rgb == min_rgb:\n",
    "        h = 0\n",
    "    \n",
    "    # if max_rgb==r then h is computed as follows\n",
    "    elif max_rgb == r:\n",
    "        h = (60 * ((g - b) / difference) + 360) % 360\n",
    "    \n",
    "    # if max_rgb==g then compute h as follows\n",
    "    elif max_rgb == g:\n",
    "        h = (60 * ((b - r) / difference) + 120) % 360\n",
    "    \n",
    "    # if max_rgb=b then compute h\n",
    "    elif max_rgb == b:\n",
    "        h = (60 * ((r - g) / difference) + 240) % 360\n",
    "    \n",
    "    # if max_rgb==zero then s=0\n",
    "    if max_rgb == 0:\n",
    "        s = 0\n",
    "    else:\n",
    "        s = (difference / max_rgb) * 100\n",
    "    \n",
    "    # compute v\n",
    "    v = max_rgb * 100\n",
    "    # return rounded values of H, S and V\n",
    "    return tuple(map(round, (h, s, v)))\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def obtain_hsv_colors(img):\n",
    "    colors = extcolors.extract_from_path(img, tolerance=7, limit=7)\n",
    "    colors = [(rgb2hsv(h[0][0], h[0][1], h[0][2]), h[1]) for h in colors[0] if h[0] != (0,0,0)]\n",
    "    return colors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = obtain_hsv_colors(images_textiles[0])\n",
    "print(colors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for col in colors:\n",
    "    print(get_color_rank(*col[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for img in images_textiles[:30]:\n",
    "    colors = obtain_hsv_colors(img)\n",
    "    plt.imshow(plt.imread(img))\n",
    "    plt.show()\n",
    "    for col in colors:\n",
    "        print(col[0])\n",
    "        print(get_color_rank(*col[0]))\n",
    "        \n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### use for training only images with medium saturation and value\n",
    "\n",
    "use codes and not only hue for color categorization\n",
    "or remove colors that are creater with black and whites"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "art-reco_x86",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}