Farbnamen werden nun in Colo-Objekte geparsed

`Color.parseString(String)` liest nun eine Datei mit Farbnamen und Hexcode Kombinationen ein. Wird der String in der Liste der Farbnamen gefunden, wird aus dem entsprechenden Hexcode ein `Color`-Objekt erzeugt.
This commit is contained in:
ngb 2022-11-29 10:11:43 +01:00
parent ec30afd441
commit 47827683e8
3 changed files with 406 additions and 16 deletions

View File

@ -4,6 +4,10 @@ import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Repräsentiert eine Farbe in der Zeichenmaschine.
@ -221,19 +225,37 @@ public class Color implements Paint {
return Color.getRGBColor(rgb);
}
public static Color parseString( String pColor ) {
pColor = pColor.toLowerCase().strip();
if( pColor.contains("red") || pColor.contains("rot") ) {
return Color.RED.copy();
} else if( pColor.contains("blue") || pColor.contains("blau") ) {
return Color.BLUE.copy();
} else if( pColor.contains("green") || pColor.contains("grün") || pColor.contains("gruen") ) {
return Color.GREEN.copy();
} else if( pColor.contains("yellow") || pColor.contains("gelb") ) {
return Color.YELLOW.copy();
} else {
return new Color();
/**
* Erstellt aus einem Farbnamen ein Farbobjekt.
* <p>
* Die gültigen Farbnamen können unter <a
* href="https://htmlcolors.com/color-names">https://htmlcolors.com/color-names</a>
* nachgeschlagen werden.
*
* @param color Der Name einer Farbe.
* @return Ein Farbobjekt.
*/
public static Color parseString( String color ) {
color = color.toLowerCase().strip();
// Parse colornames file and return first match
try(
InputStream in = Color.class.getResourceAsStream("colornames.csv");
BufferedReader reader = new BufferedReader(new InputStreamReader(in))
) {
String line;
while( (line = reader.readLine()) != null ) {
String[] parts = line.split(",");
if( parts.length == 2 ) {
if( parts[0].equals(color) ) {
return Color.parseHexcode(parts[1]);
}
}
}
} catch( IOException ex ) {
// LOG?
}
return new Color();
}
/**

View File

@ -0,0 +1,342 @@
algae green,#64e986
aliceblue,#f0f8ff
alien green,#6cc417
antiquewhite,#faebd7
aquamarine,#7fffd4
army brown,#827b60
ash gray,#666362
avocado green,#b2c248
aztech purple,#893bff
azure,#f0ffff
baby blue,#95b9c7
bashful pink,#c25283
basket ball orange,#f88158
battleship gray,#848482
bean red,#f75d59
bee yellow,#e9ab17
beer,#fbb117
beetle green,#4c787e
beige,#f5f5dc
black cat,#413839
black cow,#4c4646
black eel,#463e3f
black,#000000
blanchedalmond,#ffebcd
blonde,#fbf6d9
blood red,#7e3517
blossom pink,#f9b7ff
blue angel,#b7ceec
blue diamond,#4ee2ec
blue dress,#157dec
blue eyes,#1569c7
blue gray,#98afc7
blue green,#7bccb5
blue hosta,#77bfc7
blue ivy,#3090c7
blue jay,#2b547e
blue koi,#659ec7
blue lagoon,#8eebec
blue lotus,#6960ec
blue orchid,#1f45fc
blue ribbon,#306eff
blue whale,#342d7e
blue zircon,#57feff
blueberry blue,#0041c2
blush pink,#e6a9ec
blush red,#e56e94
brass,#b5a642
bright gold,#fdd017
bright neon pink,#f433ff
bronze,#cd7f32
brown bear,#835c3b
brown sugar,#e2a76f
bullet shell,#af9b60
burgundy,#8c001a
burlywood,#deb887
burnt pink,#c12267
butterfly blue,#38acec
cadillac pink,#e38aae
camel brown,#c19a6b
camouflage green,#78866b
cantaloupe,#ffa62f
caramel,#c68e17
carbon gray,#625d5d
carnation pink,#f778a1
celeste,#50ebec
champagne,#f7e7ce
charcoal,#34282c
chartreuse,#8afb17
cherry red,#c24641
chestnut red,#c34a2c
chestnut,#954535
chilli pepper,#c11b17
chocolate,#c85a17
cinnamon,#c58917
cloudy gray,#6d6968
clover green,#3ea055
cobalt blue,#0020c2
coffee,#6f4e37
columbia blue,#87afc7
construction cone orange,#f87431
cookie brown,#c7a317
copper,#b87333
coral blue,#afdcec
coral,#ff7f50
corn yellow,#fff380
cornflower blue,#6495ed
cornsilk,#fff8dc
cotton candy,#fcdfff
cranberry,#9f000f
cream,#ffffcc
crimson,#e238ec
crocus purple,#9172ec
crystal blue,#5cb3ff
cyan opaque,#92c7c7
cyan or aqua,#00ffff
dark carnation pink,#c12283
dark forest green,#254117
dark goldenrod,#af7817
dark orange,#f88017
dark orchid,#7d1b7e
dark salmon,#e18b6b
dark sea green,#8bb381
dark slate blue,#2b3856
dark slate grey,#25383c
dark turquoise,#3b9c9c
dark violet,#842dce
day sky blue,#82caff
deep peach,#ffcba4
deep pink,#f52887
deep sky blue,#3bb9ff
denim blue,#79baec
denim dark blue,#151b8d
desert sand,#edc9af
dimorphotheca magenta,#e3319d
dodger blue,#1589ff
dollar bill green,#85bb65
dragon green,#6afb92
dull purple,#7f525d
earth blue,#0000a0
eggplant,#614051
electric blue,#9afeff
emerald green,#5ffb17
fall leaf brown,#c8b560
fern green,#667c26
ferrari red,#f70d1a
fire engine red,#f62817
firebrick,#800517
flamingo pink,#f9a7b0
forest green,#4e9258
frog green,#99c68e
ginger brown,#c9be62
glacial blue ice,#368bc1
golden brown,#eac117
goldenrod,#edda74
granite,#837e7c
grape,#5e5a80
grapefruit,#dc381f
gray cloud,#b6b6b4
gray dolphin,#5c5858
gray goose,#d1d0ce
gray wolf,#504a4b
gray,#736f6e
grayish turquoise,#5e7d7e
green apple,#4cc417
green onion,#6aa121
green peas,#89c35c
green snake,#6cbb3c
green thumb,#b5eaaa
green yellow,#b1fb17
green,#00ff00
greenish blue,#307d7e
gunmetal,#2c3539
halloween orange,#e66c2c
harvest gold,#ede275
hazel green,#617c58
heliotrope purple,#d462ff
hot pink,#f660ab
hummingbird green,#7fe817
iceberg,#56a5ec
iguana green,#9cb071
indigo,#4b0082
iridium,#3d3c3a
jade green,#5efb6e
jasmine purple,#a23bec
jeans blue,#a0cfec
jellyfish,#46c7c7
jet gray,#616d7e
jungle green,#347c2c
kelly green,#4cc552
khaki rose,#c5908e
khaki,#ada96e
lapis blue,#15317e
lava red,#e42217
lavender blue,#e3e4fa
lavender pinocchio,#ebdde2
lawn green,#87f717
lemon chiffon,#fff8c6
light aquamarine,#93ffe8
light blue,#addfff
light coral,#e77471
light cyan,#e0ffff
light jade,#c3fdb8
light pink,#faafba
light salmon,#f9966b
light sea green,#3ea99f
light sky blue,#82cafa
light slate blue,#736aff
light slate gray,#6d7b8d
light slate,#ccffff
light steel blue,#728fce
lilac,#c8a2c8
lime green,#41a317
lipstick pink,#c48793
love red,#e41b17
lovely purple,#7f38ec
macaroni and cheese,#f2bb66
macaw blue green,#43bfc7
magenta,#ff00ff
mahogany,#c04000
mango orange,#ff8040
marble blue,#566d7e
maroon,#810541
mauve,#e0b0ff
medium aquamarine,#348781
medium forest green,#347235
medium orchid,#b048b5
medium purple,#8467d7
medium sea green,#306754
medium spring green,#348017
medium turquoise,#48cccd
medium violet red,#ca226b
metallic silver,#bcc6cc
midnight blue,#151b54
midnight,#2b1b17
milk white,#fefcff
mint green,#98ff98
mist blue,#646d7e
misty rose,#fbbbb9
moccasin,#827839
mocha,#493d26
mustard,#ffdb58
navy blue,#000080
nebula green,#59e817
neon pink,#f535aa
night,#0c090a
northern lights blue,#78c7c7
oak brown,#806517
ocean blue,#2b65ec
oil,#3b3131
orange gold,#d4a017
orange salmon,#c47451
pale blue lily,#cfecec
pale violet red,#d16587
papaya orange,#e56717
parchment,#ffffc2
pastel blue,#b4cfec
peach,#ffe5b4
pearl,#fdeef4
periwinkle,#e9cfec
pig pink,#fdd7e4
pine green,#387c44
pink bow,#c48189
pink bubblegum,#ffdfdd
pink cupcake,#e45e9d
pink daisy,#e799a3
pink lemonade,#e4287c
pink rose,#e7a1b0
pink,#faafbe
pistachio green,#9dc209
platinum,#e5e4e2
plum pie,#7d0541
plum purple,#583759
plum velvet,#7d0552
plum,#b93b8f
powder blue,#c6deff
puce,#7f5a58
pumpkin orange,#f87217
purple amethyst,#6c2dc7
purple daffodil,#b041ff
purple dragon,#c38ec7
purple flower,#a74ac7
purple haze,#4e387e
purple iris,#571b7e
purple jam,#6a287e
purple mimosa,#9e7bff
purple monster,#461b7e
purple sage bush,#7a5dc7
purple,#8e35ef
red dirt,#7f5217
red fox,#c35817
red wine,#990012
red,#ff0000
robin egg blue,#bdedff
rogue pink,#c12869
rose gold,#ecc5c0
rose,#e8adaa
rosy brown,#b38481
rosy finch,#7f4e52
royal blue,#2b60de
rubber ducky yellow,#ffd801
ruby red,#f62217
rust,#c36241
saffron,#fbb917
sage green,#848b79
salad green,#a1c935
sand,#c2b280
sandstone,#786d5f
sandy brown,#ee9a4d
sangria,#7e3817
sapphire blue,#2554c7
scarlet,#ff2400
school bus yellow,#e8a317
sea blue,#c2dfff
sea green,#4e8975
sea turtle green,#438d80
seashell,#fff5ee
seaweed green,#437c17
sedona,#cc6600
sepia,#7f462c
shamrock green,#347c17
shocking orange,#e55b3c
sienna,#8a4117
silk blue,#488ac7
sky blue,#6698ff
slate blue,#737ca1
slate gray,#657383
slime green,#bce954
smokey gray,#726e6d
spring green,#4aa02c
steel blue,#4863a0
stoplight go green,#57e964
sun yellow,#ffe87c
sunrise orange,#e67451
tan brown,#ece5b6
tangerine,#e78a61
taupe,#483c32
tea green,#ccfb5d
teal,#008080
thistle,#d2b9d3
tiffany blue,#81d8d0
tiger orange,#c88141
tron blue,#7dfdfe
tulip pink,#c25a7c
turquoise,#43c6db
tyrian purple,#c45aec
valentine red,#e55451
vampire gray,#565051
vanilla,#f3e5ab
velvet maroon,#7e354d
venom green,#728c00
viola purple,#7e587e
violet red,#f6358a
violet,#8d38c9
water,#ebf4fa
watermelon pink,#fc6c85
white,#ffffff
windows blue,#357ec7
wisteria purple,#c6aec7
wood,#966f33
yellow green,#52d017
yellow,#ffff00
zombie green,#54c571
1 algae green #64e986
2 aliceblue #f0f8ff
3 alien green #6cc417
4 antiquewhite #faebd7
5 aquamarine #7fffd4
6 army brown #827b60
7 ash gray #666362
8 avocado green #b2c248
9 aztech purple #893bff
10 azure #f0ffff
11 baby blue #95b9c7
12 bashful pink #c25283
13 basket ball orange #f88158
14 battleship gray #848482
15 bean red #f75d59
16 bee yellow #e9ab17
17 beer #fbb117
18 beetle green #4c787e
19 beige #f5f5dc
20 black cat #413839
21 black cow #4c4646
22 black eel #463e3f
23 black #000000
24 blanchedalmond #ffebcd
25 blonde #fbf6d9
26 blood red #7e3517
27 blossom pink #f9b7ff
28 blue angel #b7ceec
29 blue diamond #4ee2ec
30 blue dress #157dec
31 blue eyes #1569c7
32 blue gray #98afc7
33 blue green #7bccb5
34 blue hosta #77bfc7
35 blue ivy #3090c7
36 blue jay #2b547e
37 blue koi #659ec7
38 blue lagoon #8eebec
39 blue lotus #6960ec
40 blue orchid #1f45fc
41 blue ribbon #306eff
42 blue whale #342d7e
43 blue zircon #57feff
44 blueberry blue #0041c2
45 blush pink #e6a9ec
46 blush red #e56e94
47 brass #b5a642
48 bright gold #fdd017
49 bright neon pink #f433ff
50 bronze #cd7f32
51 brown bear #835c3b
52 brown sugar #e2a76f
53 bullet shell #af9b60
54 burgundy #8c001a
55 burlywood #deb887
56 burnt pink #c12267
57 butterfly blue #38acec
58 cadillac pink #e38aae
59 camel brown #c19a6b
60 camouflage green #78866b
61 cantaloupe #ffa62f
62 caramel #c68e17
63 carbon gray #625d5d
64 carnation pink #f778a1
65 celeste #50ebec
66 champagne #f7e7ce
67 charcoal #34282c
68 chartreuse #8afb17
69 cherry red #c24641
70 chestnut red #c34a2c
71 chestnut #954535
72 chilli pepper #c11b17
73 chocolate #c85a17
74 cinnamon #c58917
75 cloudy gray #6d6968
76 clover green #3ea055
77 cobalt blue #0020c2
78 coffee #6f4e37
79 columbia blue #87afc7
80 construction cone orange #f87431
81 cookie brown #c7a317
82 copper #b87333
83 coral blue #afdcec
84 coral #ff7f50
85 corn yellow #fff380
86 cornflower blue #6495ed
87 cornsilk #fff8dc
88 cotton candy #fcdfff
89 cranberry #9f000f
90 cream #ffffcc
91 crimson #e238ec
92 crocus purple #9172ec
93 crystal blue #5cb3ff
94 cyan opaque #92c7c7
95 cyan or aqua #00ffff
96 dark carnation pink #c12283
97 dark forest green #254117
98 dark goldenrod #af7817
99 dark orange #f88017
100 dark orchid #7d1b7e
101 dark salmon #e18b6b
102 dark sea green #8bb381
103 dark slate blue #2b3856
104 dark slate grey #25383c
105 dark turquoise #3b9c9c
106 dark violet #842dce
107 day sky blue #82caff
108 deep peach #ffcba4
109 deep pink #f52887
110 deep sky blue #3bb9ff
111 denim blue #79baec
112 denim dark blue #151b8d
113 desert sand #edc9af
114 dimorphotheca magenta #e3319d
115 dodger blue #1589ff
116 dollar bill green #85bb65
117 dragon green #6afb92
118 dull purple #7f525d
119 earth blue #0000a0
120 eggplant #614051
121 electric blue #9afeff
122 emerald green #5ffb17
123 fall leaf brown #c8b560
124 fern green #667c26
125 ferrari red #f70d1a
126 fire engine red #f62817
127 firebrick #800517
128 flamingo pink #f9a7b0
129 forest green #4e9258
130 frog green #99c68e
131 ginger brown #c9be62
132 glacial blue ice #368bc1
133 golden brown #eac117
134 goldenrod #edda74
135 granite #837e7c
136 grape #5e5a80
137 grapefruit #dc381f
138 gray cloud #b6b6b4
139 gray dolphin #5c5858
140 gray goose #d1d0ce
141 gray wolf #504a4b
142 gray #736f6e
143 grayish turquoise #5e7d7e
144 green apple #4cc417
145 green onion #6aa121
146 green peas #89c35c
147 green snake #6cbb3c
148 green thumb #b5eaaa
149 green yellow #b1fb17
150 green #00ff00
151 greenish blue #307d7e
152 gunmetal #2c3539
153 halloween orange #e66c2c
154 harvest gold #ede275
155 hazel green #617c58
156 heliotrope purple #d462ff
157 hot pink #f660ab
158 hummingbird green #7fe817
159 iceberg #56a5ec
160 iguana green #9cb071
161 indigo #4b0082
162 iridium #3d3c3a
163 jade green #5efb6e
164 jasmine purple #a23bec
165 jeans blue #a0cfec
166 jellyfish #46c7c7
167 jet gray #616d7e
168 jungle green #347c2c
169 kelly green #4cc552
170 khaki rose #c5908e
171 khaki #ada96e
172 lapis blue #15317e
173 lava red #e42217
174 lavender blue #e3e4fa
175 lavender pinocchio #ebdde2
176 lawn green #87f717
177 lemon chiffon #fff8c6
178 light aquamarine #93ffe8
179 light blue #addfff
180 light coral #e77471
181 light cyan #e0ffff
182 light jade #c3fdb8
183 light pink #faafba
184 light salmon #f9966b
185 light sea green #3ea99f
186 light sky blue #82cafa
187 light slate blue #736aff
188 light slate gray #6d7b8d
189 light slate #ccffff
190 light steel blue #728fce
191 lilac #c8a2c8
192 lime green #41a317
193 lipstick pink #c48793
194 love red #e41b17
195 lovely purple #7f38ec
196 macaroni and cheese #f2bb66
197 macaw blue green #43bfc7
198 magenta #ff00ff
199 mahogany #c04000
200 mango orange #ff8040
201 marble blue #566d7e
202 maroon #810541
203 mauve #e0b0ff
204 medium aquamarine #348781
205 medium forest green #347235
206 medium orchid #b048b5
207 medium purple #8467d7
208 medium sea green #306754
209 medium spring green #348017
210 medium turquoise #48cccd
211 medium violet red #ca226b
212 metallic silver #bcc6cc
213 midnight blue #151b54
214 midnight #2b1b17
215 milk white #fefcff
216 mint green #98ff98
217 mist blue #646d7e
218 misty rose #fbbbb9
219 moccasin #827839
220 mocha #493d26
221 mustard #ffdb58
222 navy blue #000080
223 nebula green #59e817
224 neon pink #f535aa
225 night #0c090a
226 northern lights blue #78c7c7
227 oak brown #806517
228 ocean blue #2b65ec
229 oil #3b3131
230 orange gold #d4a017
231 orange salmon #c47451
232 pale blue lily #cfecec
233 pale violet red #d16587
234 papaya orange #e56717
235 parchment #ffffc2
236 pastel blue #b4cfec
237 peach #ffe5b4
238 pearl #fdeef4
239 periwinkle #e9cfec
240 pig pink #fdd7e4
241 pine green #387c44
242 pink bow #c48189
243 pink bubblegum #ffdfdd
244 pink cupcake #e45e9d
245 pink daisy #e799a3
246 pink lemonade #e4287c
247 pink rose #e7a1b0
248 pink #faafbe
249 pistachio green #9dc209
250 platinum #e5e4e2
251 plum pie #7d0541
252 plum purple #583759
253 plum velvet #7d0552
254 plum #b93b8f
255 powder blue #c6deff
256 puce #7f5a58
257 pumpkin orange #f87217
258 purple amethyst #6c2dc7
259 purple daffodil #b041ff
260 purple dragon #c38ec7
261 purple flower #a74ac7
262 purple haze #4e387e
263 purple iris #571b7e
264 purple jam #6a287e
265 purple mimosa #9e7bff
266 purple monster #461b7e
267 purple sage bush #7a5dc7
268 purple #8e35ef
269 red dirt #7f5217
270 red fox #c35817
271 red wine #990012
272 red #ff0000
273 robin egg blue #bdedff
274 rogue pink #c12869
275 rose gold #ecc5c0
276 rose #e8adaa
277 rosy brown #b38481
278 rosy finch #7f4e52
279 royal blue #2b60de
280 rubber ducky yellow #ffd801
281 ruby red #f62217
282 rust #c36241
283 saffron #fbb917
284 sage green #848b79
285 salad green #a1c935
286 sand #c2b280
287 sandstone #786d5f
288 sandy brown #ee9a4d
289 sangria #7e3817
290 sapphire blue #2554c7
291 scarlet #ff2400
292 school bus yellow #e8a317
293 sea blue #c2dfff
294 sea green #4e8975
295 sea turtle green #438d80
296 seashell #fff5ee
297 seaweed green #437c17
298 sedona #cc6600
299 sepia #7f462c
300 shamrock green #347c17
301 shocking orange #e55b3c
302 sienna #8a4117
303 silk blue #488ac7
304 sky blue #6698ff
305 slate blue #737ca1
306 slate gray #657383
307 slime green #bce954
308 smokey gray #726e6d
309 spring green #4aa02c
310 steel blue #4863a0
311 stoplight go green #57e964
312 sun yellow #ffe87c
313 sunrise orange #e67451
314 tan brown #ece5b6
315 tangerine #e78a61
316 taupe #483c32
317 tea green #ccfb5d
318 teal #008080
319 thistle #d2b9d3
320 tiffany blue #81d8d0
321 tiger orange #c88141
322 tron blue #7dfdfe
323 tulip pink #c25a7c
324 turquoise #43c6db
325 tyrian purple #c45aec
326 valentine red #e55451
327 vampire gray #565051
328 vanilla #f3e5ab
329 velvet maroon #7e354d
330 venom green #728c00
331 viola purple #7e587e
332 violet red #f6358a
333 violet #8d38c9
334 water #ebf4fa
335 watermelon pink #fc6c85
336 white #ffffff
337 windows blue #357ec7
338 wisteria purple #c6aec7
339 wood #966f33
340 yellow green #52d017
341 yellow #ffff00
342 zombie green #54c571

View File

@ -147,6 +147,36 @@ class ColorTest {
@Test
void parseHexcode() {
Color c;
c = Color.parseHexcode("#f05025");
assertEquals(Color.RED, c);
c = Color.parseHexcode("#F8EF22");
assertEquals(Color.YELLOW, c);
c = Color.parseHexcode("#FF40ff");
assertEquals(new Color(255, 64, 255), c);
// TODO: Test hexcodes of length 3, 6 and 9
// TODO: Test malformed hexcodes
}
@Test
void parseString() {
Color c;
c = Color.parseString("red");
assertEquals(new Color(255, 0, 0), c);
c = Color.parseString("zombie green");
assertEquals(Color.parseHexcode("#54c571"), c);
c = Color.parseString("algae green");
assertEquals(new Color(100, 233, 134), c);
c = Color.parseString("light slate gray");
assertEquals(Color.parseHexcode("#6d7b8d"), c);
}
@Test
@ -173,10 +203,6 @@ class ColorTest {
void HSLtoRGB() {
}
@Test
void testHSLtoRGB() {
}
@Test
void copy() {
}