1
+ package org .codeoverflow .chatoverflow .requirement .parameter
2
+
3
+ import org .codeoverflow .chatoverflow .api .io .parameter .ColorParameter
4
+ import org .codeoverflow .chatoverflow .registry .Impl
5
+ import java .awt .Color
6
+
7
+ /**
8
+ * A parameter holding a Color value.
9
+ */
10
+ @ Impl (impl = classOf [ColorParameter ])
11
+ class ColorParameterImpl extends ColorParameter {
12
+ private var value : Color = null
13
+
14
+ override def getType : Class [Color ] = classOf [Color ]
15
+
16
+ override def serialize (): String = s " ${value.getRed}, ${value.getGreen}, ${value.getBlue}, ${value.getAlpha}"
17
+
18
+ override def get (): Color = value
19
+
20
+ override def deserialize (value : String ): Unit = {
21
+
22
+ val hex3 = " ^#([a-fA-F0-9]{6})$" .r
23
+ val hex4 = " ^#([a-fA-F0-9]{8})$" .r
24
+ val int3 = " ^(\\ d+),(\\ d+),(\\ d+)$" .r
25
+ val int4 = " ^(\\ d+),(\\ d+),(\\ d+),(\\ d+)$" .r
26
+ val float3 = " ^(\\ d+(?:\\ .\\ d+)?),(\\ d+(?:\\ .\\ d+)?),(\\ d+(?:\\ .\\ d+)?)$" .r
27
+ val float4 = " ^(\\ d+(?:\\ .\\ d+)?),(\\ d+(?:\\ .\\ d+)?),(\\ d+(?:\\ .\\ d+)?),(\\ d+(?:\\ .\\ d+)?)$" .r
28
+
29
+ try {
30
+ value match {
31
+ case hex3(hex) => {
32
+ set(new Color (Integer .valueOf(hex.substring(0 , 2 ), 16 ),
33
+ Integer .valueOf(hex.substring(2 , 4 ), 16 ),
34
+ Integer .valueOf(hex.substring(4 , 6 ), 16 )))
35
+ }
36
+ case hex4(hex) =>
37
+ set(new Color (Integer .valueOf(hex.substring(0 , 2 ), 16 ),
38
+ Integer .valueOf(hex.substring(2 , 4 ), 16 ),
39
+ Integer .valueOf(hex.substring(4 , 6 ), 16 ),
40
+ Integer .valueOf(hex.substring(6 , 8 ), 16 )))
41
+ case int3(r, g, b) => set(new Color (r.toInt, g.toInt, b.toInt))
42
+ case int4(r, g, b, a) => set(new Color (r.toInt, g.toInt, b.toInt, a.toInt))
43
+ case float3(r, g, b) => set(new Color (r.toFloat, g.toFloat, b.toFloat))
44
+ case float4(r, g, b, a) => set(new Color (r.toFloat, g.toFloat, b.toFloat, a.toFloat))
45
+ }
46
+ } catch {
47
+ case _ : Exception => print(" Can't convert String to color" )
48
+ }
49
+ }
50
+
51
+ override def set (value : Color ): Unit = this .value = value
52
+
53
+ }
0 commit comments