Bimba.git

ref: 69173a3d7b96203c44ff3114a13da1feed7eccfc

app/src/main/java/xyz/apiote/bimba/czwek/repo/Colour.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
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.repo

import xyz.apiote.bimba.czwek.api.ColourV1

data class Colour(val R: UByte, val G: UByte, val B: UByte) {
	constructor(c: ColourV1) : this(c.R, c.G, c.B)

	fun toInt(): Int {
		var rgb = 0xff
		rgb = (rgb shl 8) + R.toInt()
		rgb = (rgb shl 8) + G.toInt()
		rgb = (rgb shl 8) + B.toInt()
		return rgb
	}

	companion object {
		fun fromHex(hex: String?): Colour {
			if (hex == null) return Colour(255u, 255u, 255u)
			return hex.removePrefix("#").let {
				if (it.isEmpty()) {
					Colour(255u, 255u, 255u)
				} else {
					val r = it.substring(0 until 2).toUByte(16)
					val g = it.substring(2 until 4).toUByte(16)
					val b = it.substring(4 until 6).toUByte(16)
					Colour(r, g, b)
				}
			}
		}
	}
}