-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.kt
158 lines (142 loc) · 5.61 KB
/
Utils.kt
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
/******************************************************************************
* Copyright (C) 2020 Jan Kubovy ([email protected]) *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
******************************************************************************/
@file:Suppress("unused")
package com.poterion.communication.serial
import com.poterion.communication.serial.payload.ColorOrder
import com.poterion.communication.serial.payload.RgbColor
import java.awt.Color
import java.nio.charset.Charset
/**
* Converts 8 bools to one byte.
*
* @param b7 Bit 7
* @param b6 Bit 6
* @param b5 Bit 5
* @param b4 Bit 4
* @param b3 Bit 3
* @param b2 Bit 2
* @param b1 Bit 1
* @param b0 Bit 0
* @return Composed byte.
* @author Jan Kubovy [[email protected]]
*/
fun bools2Byte(b7: Boolean, b6: Boolean, b5: Boolean, b4: Boolean, b3: Boolean, b2: Boolean, b1: Boolean, b0: Boolean) =
(if (b7) 0b10000000 else 0b00000000) or
(if (b6) 0b01000000 else 0b00000000) or
(if (b5) 0b00100000 else 0b00000000) or
(if (b4) 0b00010000 else 0b00000000) or
(if (b3) 0b00001000 else 0b00000000) or
(if (b2) 0b00000100 else 0b00000000) or
(if (b1) 0b00000010 else 0b00000000) or
(if (b0) 0b00000001 else 0b00000000)
/**
* Converts a byte to an list of 8 bools.
*
* @param byte Byte to convert.
* @return List containing 8 bools corresponding to the 8 bits in a byte.
* @author Jan Kubovy [[email protected]]
*/
fun byte2Bools(byte: Int) = listOf(
(byte and 0b00000001) > 0,
(byte and 0b00000010) > 0,
(byte and 0b00000100) > 0,
(byte and 0b00001000) > 0,
(byte and 0b00010000) > 0,
(byte and 0b00100000) > 0,
(byte and 0b01000000) > 0,
(byte and 0b10000000) > 0)
/**
* Calculates a checksum.
* @author Jan Kubovy [[email protected]]
*/
fun ByteArray.calculateChecksum() =
(map { it.toInt() }.takeIf { it.isNotEmpty() }?.reduce { acc, i -> (acc + i) and 0xFF } ?: 0) and 0xFF
/**
* Convert a pair of bytes to a double byte.
* @author Jan Kubovy [[email protected]]
*/
fun Pair<Byte, Byte>.toInt() = let { first.toInt() to second.toInt() }.toDoubleInt()
/**
* Convert a pair of bytes, each represented by an [Int], to an double byte.
* @author Jan Kubovy [[email protected]]
*/
fun Pair<Int, Int>.toDoubleInt() = let { ((it.first shl 8) and 0xFF00) or (it.second and 0xFF) }
/**
* Convert a byte, represented by an [Int], to a byte array.
* @author Jan Kubovy [[email protected]]
*/
fun Int.toByteArray() = listOf(this).toByteArray()
/**
* Convert a list of bytes, represented by [Int], to byte array.
* @author Jan Kubovy [[email protected]]
*/
fun List<Int>.toByteArray() = this.map { it.toByte() }.toByteArray()
/**
* Convert a [IntArray], each [Int] representing a [Byte], to ASCII encoded string.
* @author Jan Kubovy [[email protected]]
*/
fun IntArray.toString(charset: Charset) = map { it.toByte() }.toByteArray().toString(charset)
/**
* Converts a hex-encoded color to [RgbColor].
* @author Jan Kubovy [[email protected]]
*/
fun String.toRGBColor(): RgbColor? = "^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})".toRegex()
.matchEntire(this)
?.groupValues
?.mapNotNull { it.toIntOrNull(16) }
?.takeIf { it.size == 3 }
?.let { RgbColor(it[0], it[1], it[2]) }
/**
* Converts a [java.awt.Color] to [RgbColor].
* @author Jan Kubovy [[email protected]]
*/
fun Color.toRGBColor(): RgbColor = RgbColor(red, green, blue)
/**
* Converts a [java.awt.Color] to hex-encoded color string.
* @author Jan Kubovy [[email protected]]
*/
fun Color.toHex(prefix: String = "#") = toRGBColor().toHex(prefix)
/**
* Converts [RgbColor] to [java.awt.Color].
* @author Jan Kubovy [[email protected]]
*/
fun RgbColor.toAwtColor() = Color(red, green, blue)
/**
* Converts [RgbColor] to an array of RGB components.
* @param colorOrder [ColorOrder]
* @author Jan Kubovy [[email protected]]
*/
fun RgbColor.toComponents(colorOrder: ColorOrder = ColorOrder.RGB) = when (colorOrder) {
ColorOrder.RGB -> intArrayOf(red, green, blue)
ColorOrder.GRB -> intArrayOf(green, red, blue)
}
/**
* Converts an array of RGB components to [RgbColor]
* @param colorOrder [ColorOrder]
* @param from Starting at index (default `0`)
* @author Jan Kubovy [[email protected]]
*/
fun IntArray.toRgbColor(colorOrder: ColorOrder = ColorOrder.RGB, from: Int = 0) = when (colorOrder) {
ColorOrder.RGB -> RgbColor(this[from], this[from + 1], this[from + 2])
ColorOrder.GRB -> RgbColor(this[from + 1], this[from], this[from + 2])
}
/**
* Converts [RgbColor] to hex-encoded color string.
* @author Jan Kubovy [[email protected]]
*/
fun RgbColor.toHex(prefix: String = "#") = listOf(red, green, blue).joinToString("", prefix) { "%02X".format(it) }