-
Notifications
You must be signed in to change notification settings - Fork 0
/
zephr_math.odin
160 lines (132 loc) · 3.67 KB
/
zephr_math.odin
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
package zephr
import "base:intrinsics"
import "core:math"
import m "core:math/linalg/glsl"
Color :: struct {
r, g, b, a: u8,
}
orthographic_projection_2d :: proc(left, right, bottom, top: f32) -> m.mat4 {
result := m.identity(m.mat4)
result[0][0] = 2 / (right - left)
result[3][0] = -(right + left) / (right - left)
result[1][1] = 2 / (top - bottom)
result[3][1] = -(top + bottom) / (top - bottom)
result[2][2] = -1
result[3][3] = 1
return result
}
mult_color :: proc(color: Color, scalar: f32) -> Color {
color := color
color.r = clamp(cast(u8)(cast(f32)color.r * scalar), 0, 255)
color.g = clamp(cast(u8)(cast(f32)color.g * scalar), 0, 255)
color.b = clamp(cast(u8)(cast(f32)color.b * scalar), 0, 255)
return color
}
hsv2rgb :: proc(h: f32, s: f32, v: f32) -> Color {
c := v * s
x := (c * (1 - abs(math.mod(h / 60.0, 2) - 1)))
m := v - c
r, g, b: f32
if (h >= 0 && h < 60) {
r = c
g = x
b = 0
} else if (h >= 60 && h < 120) {
r = x
g = c
b = 0
} else if (h >= 120 && h < 180) {
r = 0
g = c
b = x
} else if (h >= 180 && h < 240) {
r = 0
g = x
b = c
} else if (h >= 240 && h < 300) {
r = x
g = 0
b = c
} else {
r = c
g = 0
b = x
}
return (Color){(u8)((r + m) * 255), (u8)((g + m) * 255), (u8)((b + m) * 255), 255}
}
determine_color_contrast :: proc(bg: Color) -> Color {
white_contrast := get_contrast(bg, COLOR_WHITE)
black_contrast := get_contrast(bg, COLOR_BLACK)
return white_contrast > black_contrast ? COLOR_WHITE : COLOR_BLACK
}
@(private)
get_srgb :: proc(component: f32) -> f32 {
return (component / 255 <= 0.03928) ? component / 255 / 12.92 : math.pow((component / 255 + 0.055) / 1.055, 2.4)
}
get_luminance :: proc(color: Color) -> f32 {
return(
((0.2126 * get_srgb(cast(f32)color.r)) +
(0.7152 * get_srgb(cast(f32)color.g)) +
(0.0722 * get_srgb(cast(f32)color.b))) /
255 \
)
}
@(private)
get_contrast :: proc(fg: Color, bg: Color) -> f32 {
l1 := get_luminance(fg)
l2 := get_luminance(bg)
return (max(l1, l2) + 0.05) / (min(l1, l2) + 0.05)
}
//
//
// Easing functions
//
//
ease_out_circ :: proc(t: $T) -> T where intrinsics.type_is_numeric(T) {
return math.sqrt(1 - math.pow(t - 1, 2))
}
ease_out_cubic :: proc(t: $T) -> T where intrinsics.type_is_numeric(T) {
return 1 - math.pow(1 - t, 3)
}
ease_out_elastic :: proc(t: $T) -> T where intrinsics.type_is_numeric(T) {
c4: T = (2 * math.PI) / 3
if t == 0 {
return 0
} else if t == 1 {
return 1
} else {
return math.pow(2, -10 * t) * math.sin_f32((t * 10 - 0.75) * c4) + 1
}
}
ease_in_quint :: proc(t: $T) -> T where intrinsics.type_is_numeric(T) {
return math.pow(t, 5)
}
ease_out_bounce :: proc(t: $T) -> T where intrinsics.type_is_numeric(T) {
t := t
n1: T = 7.5625
d1: T = 2.75
if (t < 1 / d1) {
return n1 * t * t
} else if (t < 2 / d1) {
t -= 1.5 / d1
return n1 * (t) * t + 0.75
} else if (t < 2.5 / d1) {
t -= 2.25 / d1
return n1 * (t) * t + 0.9375
} else {
t -= 2.625 / d1
return n1 * (t) * t + 0.984375
}
}
ease_in_out_quint :: proc(t: f32) -> f32 {
return t < 0.5 ? 16 * t * t * t * t * t : 1 - math.pow(-2 * t + 2, 5) / 2
}
ease_in_out_back :: proc(t: f64) -> f64 {
c1 := 1.70158
c2 := c1 * 1.525
if t < 0.5 {
return (math.pow(2 * t, 2) * ((c2 + 1) * 2 * t - c2)) / 2
} else {
return (math.pow(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2
}
}