summaryrefslogtreecommitdiffstats
path: root/research/oop.sh
blob: 5e5dbf697aefd9796a9945da77eae70eb1e58ad5 (plain)
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
array_n=0

array_new()
{
	array_n=$((${array_n} + 1))
	retval="__arr_${array_n}"
}

array_set()
{
	local arr="${1}"
	local idx="${2}"
	local val="${3}"

	eval "__arr_${arr}_${idx}=\${val}"
}

array_get()
{
	local arr="${1}"
	local idx="${2}"

	eval "retval=\${__arr_${arr}_${idx}}"
}

array_call()
{
	local arr="${1}"
	local idx="${2}"
	local fn=

	eval "fn=\${__arr_${arr}_${idx}}"
	"${fn}" ${arr}
}

color_new()
{
	local r="${1}"
	local g="${2}"
	local b="${3}"
	local self=

	array_new
	self=${retval}
	array_set ${self} r "${r}"
	array_set ${self} g "${g}"
	array_set ${self} b "${b}"
	array_set ${self} print color_print

	retval=${self}
}

color_get_r()
{
	local self="${1}"

	array_get ${self} r
}

color_get_g()
{
	local self="${1}"

	array_get ${self} g
}

color_get_b()
{
	local self="${1}"

	array_get ${self} b
}

color_print()
{
	local self="${1}"

	array_get ${self} r
	printf '(%d, ' ${retval}
	array_get ${self} g
	printf '%d, ' ${retval}
	array_get ${self} b
	printf '%d)' ${retval}
}

color_new 255 0 0
c=${retval}
color_get_r ${c}
printf 'R: %d\n' ${retval}
color_get_g ${c}
printf 'G: %d\n' ${retval}
color_get_b ${c}
printf 'B: %d\n' ${retval}
array_call ${c} print