Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5c2f1e53 authored by Fynn Godau's avatar Fynn Godau
Browse files

Parse hsla values

parent a5685a0f
Loading
Loading
Loading
Loading
+22 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ import org.microg.gms.maps.MapsConstants
import org.microg.gms.maps.mapbox.utils.MapContext
import java.lang.NumberFormatException
import kotlin.math.pow
import kotlin.math.roundToInt

const val TAG = "GmsMapStyles"
const val KEY_METADATA_FEATURE_TYPE = "microg:gms-type-feature"
@@ -130,7 +131,7 @@ class Styler(
/**
 * Returns true if string is likely to contain a color.
 */
fun String.isColor() = startsWith("hsl(") || startsWith("#") || startsWith("rgba(")
fun String.isColor() = startsWith("hsl(") || startsWith("hsla(") || startsWith("#") || startsWith("rgba(")

/**
 * Can parse colors in the format '#rrggbb', '#aarrggbb', 'hsl(h, s, l)', and 'rgba(r, g, b, a)'
@@ -159,6 +160,26 @@ fun String.parseColor(): Int {
            Log.w(TAG, "Invalid color `$this`")
            0
        }
    } else if (startsWith("hsla(")) {
        val hslArray = replace("hsla(", "").replace(")", "").split(", ")
        if (hslArray.size != 4) {
            Log.w(TAG, "Invalid color `$this`")
            return 0
        }

        return try {
            ColorUtils.setAlphaComponent(
                ColorUtils.HSLToColor(
                    floatArrayOf(
                        hslArray[0].toFloat(), hslArray[1].parseFloat(), hslArray[2].parseFloat()
                    )
                ), (hslArray[3].parseFloat() * 255).roundToInt()
            )
        } catch (e: NumberFormatException) {
            Log.w(TAG, "Invalid color `$this`")
            0
        }

    } else if (startsWith("rgba(")) {
        return com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor(this)
    }