Bimba.git

ref: e5c7dc933fba208aba52c3dcb305b522120b4b2c

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

package xyz.apiote.bimba.czwek.repo

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.os.Parcelable
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.graphics.ColorUtils
import androidx.core.graphics.drawable.toBitmap
import kotlinx.parcelize.Parcelize
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.api.StopStub
import xyz.apiote.bimba.czwek.dpToPixelI
import java.util.zip.Adler32

@Parcelize
data class StopStub(val name: String, val nodeName: String, val code: String, val zone: String, val onDemand: Boolean) :
	Parcelable {
	constructor(stopStub: StopStub) : this(
		stopStub.name,
		stopStub.nodeName,
		stopStub.code,
		stopStub.zone,
		stopStub.onDemand
	)
	fun icon(context: Context, scale: Float = 1f): Drawable {
		val saturationArray = arrayOf(0.5f, 0.65f, 0.8f)
		val sal = saturationArray.size
		val lightnessArray = arrayOf(.5f)
		val lal = lightnessArray.size
		val md = Adler32().let {
			it.update(nodeName.toByteArray())
			it.value
		}
		val h = md % 359f
		val s = saturationArray[(md / 360 % sal).toInt()]
		val l = lightnessArray[(md / 360 / sal % lal).toInt()]
		val fg = AppCompatResources.getDrawable(context, R.drawable.stop)
		val bg = AppCompatResources.getDrawable(context, R.drawable.stop_bg)!!.mutate().apply {
			setTint(ColorUtils.HSLToColor(arrayOf(h, s, l).toFloatArray()))
		}
		return BitmapDrawable(
			context.resources,
			LayerDrawable(arrayOf(bg, fg)).mutate()
				.toBitmap(dpToPixelI(24f / scale), dpToPixelI(24f / scale), Bitmap.Config.ARGB_8888)
		)
	}
}