Skip to content

Commit 458b525

Browse files
committed
Switch to Compose color
1 parent 54bc724 commit 458b525

File tree

17 files changed

+145
-109
lines changed

17 files changed

+145
-109
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Add to your `build.gradle.kts`:
1414

1515
```kotlin
1616
dependencies {
17-
implementation("de.afarber:openmapview:0.7.0")
17+
implementation("de.afarber:openmapview:0.9.0")
1818
}
1919
```
2020

examples/Example04Polylines/src/main/kotlin/de/afarber/openmapview/example04polylines/MainActivity.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package de.afarber.openmapview.example04polylines
99

1010
import android.content.Intent
11-
import android.graphics.Color
1211
import android.net.Uri
1312
import android.os.Bundle
1413
import android.widget.Toast
@@ -19,6 +18,7 @@ import androidx.compose.material3.MaterialTheme
1918
import androidx.compose.material3.Surface
2019
import androidx.compose.runtime.Composable
2120
import androidx.compose.ui.Modifier
21+
import androidx.compose.ui.graphics.Color
2222
import androidx.compose.ui.platform.LocalContext
2323
import androidx.compose.ui.viewinterop.AndroidView
2424
import de.afarber.openmapview.LatLng
@@ -66,7 +66,7 @@ fun MapViewScreen() {
6666
LatLng(51.4650, 7.2500),
6767
LatLng(51.4620, 7.2550),
6868
),
69-
strokeColor = Color.BLUE,
69+
strokeColor = Color.Blue,
7070
strokeWidth = 8f,
7171
clickable = true,
7272
tag = "Blue Route",
@@ -83,7 +83,7 @@ fun MapViewScreen() {
8383
LatLng(51.4680, 7.2520),
8484
LatLng(51.4700, 7.2550),
8585
),
86-
strokeColor = Color.RED,
86+
strokeColor = Color.Red,
8787
strokeWidth = 6f,
8888
clickable = true,
8989
tag = "Red Path",
@@ -99,9 +99,9 @@ fun MapViewScreen() {
9999
LatLng(51.4660, 7.2420),
100100
LatLng(51.4640, 7.2420),
101101
),
102-
strokeColor = Color.rgb(0, 128, 0),
102+
strokeColor = Color(red = 0, green = 128, blue = 0),
103103
strokeWidth = 4f,
104-
fillColor = Color.argb(100, 0, 255, 0),
104+
fillColor = Color(red = 0, green = 255, blue = 0, alpha = 100),
105105
clickable = true,
106106
tag = "Green Park",
107107
),
@@ -124,9 +124,9 @@ fun MapViewScreen() {
124124
LatLng(51.4706, 7.2610),
125125
),
126126
),
127-
strokeColor = Color.CYAN,
127+
strokeColor = Color.Cyan,
128128
strokeWidth = 4f,
129-
fillColor = Color.argb(100, 0, 255, 255),
129+
fillColor = Color(red = 0, green = 255, blue = 255, alpha = 100),
130130
clickable = true,
131131
tag = "Cyan Donut",
132132
),

examples/Example08Circles/src/main/kotlin/de/afarber/openmapview/example08circles/MainActivity.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package de.afarber.openmapview.example08circles
99

1010
import android.content.Intent
11-
import android.graphics.Color
1211
import android.net.Uri
1312
import android.os.Bundle
1413
import android.widget.Toast
@@ -33,6 +32,7 @@ import androidx.compose.runtime.remember
3332
import androidx.compose.runtime.setValue
3433
import androidx.compose.ui.Alignment
3534
import androidx.compose.ui.Modifier
35+
import androidx.compose.ui.graphics.Color
3636
import androidx.compose.ui.platform.LocalContext
3737
import androidx.compose.ui.unit.dp
3838
import androidx.compose.ui.viewinterop.AndroidView
@@ -83,9 +83,9 @@ fun MapViewScreen() {
8383
CircleOptions()
8484
.center(bochumCenter)
8585
.radius(500f)
86-
.strokeColor(Color.RED)
86+
.strokeColor(Color.Red)
8787
.strokeWidth(5f)
88-
.fillColor(Color.argb(64, 255, 0, 0))
88+
.fillColor(Color(red = 255, green = 0, blue = 0, alpha = 64))
8989
.clickable(true)
9090
.zIndex(2f)
9191
.tag("Small Red Circle - 500m"),
@@ -97,9 +97,9 @@ fun MapViewScreen() {
9797
CircleOptions()
9898
.center(offset1)
9999
.radius(1000f)
100-
.strokeColor(Color.BLUE)
100+
.strokeColor(Color.Blue)
101101
.strokeWidth(8f)
102-
.fillColor(Color.argb(64, 0, 0, 255))
102+
.fillColor(Color(red = 0, green = 0, blue = 255, alpha = 64))
103103
.clickable(true)
104104
.zIndex(1f)
105105
.tag("Medium Blue Circle - 1000m"),
@@ -111,9 +111,9 @@ fun MapViewScreen() {
111111
CircleOptions()
112112
.center(offset2)
113113
.radius(1500f)
114-
.strokeColor(Color.GREEN)
114+
.strokeColor(Color.Green)
115115
.strokeWidth(10f)
116-
.fillColor(Color.argb(64, 0, 255, 0))
116+
.fillColor(Color(red = 0, green = 255, blue = 0, alpha = 64))
117117
.clickable(true)
118118
.zIndex(0f)
119119
.tag("Large Green Circle - 1500m"),
@@ -125,9 +125,9 @@ fun MapViewScreen() {
125125
Circle(
126126
center = offset3,
127127
radius = 750f,
128-
strokeColor = Color.MAGENTA,
128+
strokeColor = Color.Magenta,
129129
strokeWidth = 6f,
130-
fillColor = Color.argb(64, 255, 0, 255),
130+
fillColor = Color(red = 255, green = 0, blue = 255, alpha = 64),
131131
clickable = true,
132132
zIndex = 1.5f,
133133
tag = "Kotlin Style Circle - 750m",
@@ -144,15 +144,19 @@ fun MapViewScreen() {
144144
val randomLat = bochumCenter.latitude + (Random.nextDouble() - 0.5) * 0.03
145145
val randomLng = bochumCenter.longitude + (Random.nextDouble() - 0.5) * 0.06
146146
val randomRadius = Random.nextInt(300, 1500).toFloat()
147-
val randomColor = Color.rgb(Random.nextInt(256), Random.nextInt(256), Random.nextInt(256))
147+
val randomColor = Color(
148+
red = Random.nextInt(256),
149+
green = Random.nextInt(256),
150+
blue = Random.nextInt(256),
151+
)
148152

149153
map.addCircle(
150154
CircleOptions()
151155
.center(LatLng(randomLat, randomLng))
152156
.radius(randomRadius)
153157
.strokeColor(randomColor)
154158
.strokeWidth(Random.nextInt(3, 12).toFloat())
155-
.fillColor(Color.argb(64, Color.red(randomColor), Color.green(randomColor), Color.blue(randomColor)))
159+
.fillColor(randomColor.copy(alpha = 64f / 255f))
156160
.clickable(true)
157161
.zIndex(Random.nextFloat() * 5)
158162
.tag("Random Circle ${++circleCount} - ${randomRadius.toInt()}m"),

examples/Example11MapTypes/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ Demonstrates switching between different map types in OpenMapView.
1717

1818
OpenMapView supports several free OpenStreetMap-based tile sources:
1919

20-
| Type | Constant | Description |
21-
|------|----------|-------------|
22-
| Normal | `MapType.NORMAL` | Standard OpenStreetMap road map (default) |
23-
| Terrain | `MapType.TERRAIN` | OpenTopoMap with elevation contour lines and hillshading |
20+
| Type | Constant | Description |
21+
| ------------ | ---------------------- | -------------------------------------------------------------------- |
22+
| Normal | `MapType.NORMAL` | Standard OpenStreetMap road map (default) |
23+
| Terrain | `MapType.TERRAIN` | OpenTopoMap with elevation contour lines and hillshading |
2424
| Humanitarian | `MapType.HUMANITARIAN` | Humanitarian OSM style emphasizing hospitals, schools, water sources |
25-
| Cycle | `MapType.CYCLE` | CyclOSM showing bike lanes, paths, parking, and difficulty ratings |
26-
| None | `MapType.NONE` | No base map tiles displayed |
25+
| Cycle | `MapType.CYCLE` | CyclOSM showing bike lanes, paths, parking, and difficulty ratings |
26+
| None | `MapType.NONE` | No base map tiles displayed |
2727

2828
## Usage
2929

@@ -51,6 +51,7 @@ try {
5151
## Implementation Details
5252

5353
When the map type changes:
54+
5455
1. The tile source is updated in `MapController`
5556
2. The tile cache is cleared to prevent showing incorrect tiles
5657
3. Attribution text is updated to match the new tile source

openmapview/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ android {
6464
dependencies {
6565
implementation("androidx.core:core-ktx:1.15.0")
6666
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
67+
implementation("androidx.compose.ui:ui-graphics:1.7.6")
6768
implementation("io.ktor:ktor-client-android:2.3.7")
6869
implementation("com.jakewharton:disklrucache:2.0.2")
6970

openmapview/src/main/kotlin/de/afarber/openmapview/Circle.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
package de.afarber.openmapview
99

10-
import android.graphics.Color
10+
import androidx.compose.ui.graphics.Color
1111

1212
/**
1313
* Represents a circle on the map with a center point and radius in meters.
@@ -25,9 +25,9 @@ import android.graphics.Color
2525
data class Circle(
2626
val center: LatLng,
2727
val radius: Float,
28-
val strokeColor: Int = Color.BLACK,
28+
val strokeColor: Color = Color.Black,
2929
val strokeWidth: Float = 10f,
30-
val fillColor: Int = Color.argb(128, 128, 128, 128),
30+
val fillColor: Color = Color(red = 128, green = 128, blue = 128, alpha = 128),
3131
val visible: Boolean = true,
3232
val clickable: Boolean = false,
3333
val zIndex: Float = 0f,

openmapview/src/main/kotlin/de/afarber/openmapview/CircleOptions.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
package de.afarber.openmapview
99

10-
import android.graphics.Color
10+
import androidx.compose.ui.graphics.Color
1111

1212
/**
1313
* Builder class for creating Circle instances with a fluent API.
@@ -18,17 +18,17 @@ import android.graphics.Color
1818
* val circle = CircleOptions()
1919
* .center(LatLng(40.7128, -74.0060))
2020
* .radius(1000f)
21-
* .strokeColor(Color.RED)
22-
* .fillColor(Color.argb(128, 255, 0, 0))
21+
* .strokeColor(Color.Red)
22+
* .fillColor(Color(red = 255, green = 0, blue = 0, alpha = 128))
2323
* .clickable(true)
2424
* ```
2525
*/
2626
class CircleOptions {
2727
private var center: LatLng? = null
2828
private var radius: Float? = null
29-
private var strokeColor: Int = Color.BLACK
29+
private var strokeColor: Color = Color.Black
3030
private var strokeWidth: Float = 10f
31-
private var fillColor: Int = Color.argb(128, 128, 128, 128)
31+
private var fillColor: Color = Color(red = 128, green = 128, blue = 128, alpha = 128)
3232
private var visible: Boolean = true
3333
private var clickable: Boolean = false
3434
private var zIndex: Float = 0f
@@ -54,9 +54,9 @@ class CircleOptions {
5454

5555
/**
5656
* Sets the stroke color of the circle outline.
57-
* @param color Color value (e.g., Color.RED)
57+
* @param color Color value (e.g., Color.Red)
5858
*/
59-
fun strokeColor(color: Int): CircleOptions {
59+
fun strokeColor(color: Color): CircleOptions {
6060
this.strokeColor = color
6161
return this
6262
}
@@ -72,9 +72,9 @@ class CircleOptions {
7272

7373
/**
7474
* Sets the fill color of the circle interior.
75-
* @param color Color value (e.g., Color.argb(128, 255, 0, 0))
75+
* @param color Color value (e.g., Color(red = 255, green = 0, blue = 0, alpha = 128))
7676
*/
77-
fun fillColor(color: Int): CircleOptions {
77+
fun fillColor(color: Color): CircleOptions {
7878
this.fillColor = color
7979
return this
8080
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2025 Alexander Farber
3+
* SPDX-License-Identifier: MIT
4+
*
5+
* This file is part of the OpenMapView project (https://github.com/afarber/OpenMapView)
6+
*/
7+
8+
package de.afarber.openmapview
9+
10+
import androidx.compose.ui.graphics.Color
11+
12+
/**
13+
* Converts a CSS color string to a Compose Color.
14+
*
15+
* Supported formats:
16+
* - Hex colors: "#RGB", "#ARGB", "#RRGGBB", "#AARRGGBB"
17+
* - Named colors: "red", "blue", "green", etc.
18+
*
19+
* @return Compose Color, or null if the string cannot be parsed
20+
*
21+
* Example usage:
22+
* ```
23+
* val color1 = "#FF0000".toComposeColor() // Red
24+
* val color2 = "blue".toComposeColor() // Blue
25+
* val color3 = "#80FF0000".toComposeColor() // Semi-transparent red
26+
* ```
27+
*/
28+
fun String.toComposeColor(): Color? =
29+
try {
30+
val androidColor = android.graphics.Color.parseColor(this)
31+
Color(androidColor)
32+
} catch (e: IllegalArgumentException) {
33+
null
34+
}

openmapview/src/main/kotlin/de/afarber/openmapview/GeoJsonParser.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
package de.afarber.openmapview
99

10-
import android.graphics.Color
10+
import androidx.compose.ui.graphics.Color
1111
import org.json.JSONArray
1212
import org.json.JSONObject
1313

@@ -132,7 +132,7 @@ object GeoJsonParser {
132132
val polyline =
133133
Polyline(
134134
points = points,
135-
strokeColor = parseColor(properties?.optString("stroke")) ?: Color.BLUE,
135+
strokeColor = parseColor(properties?.optString("stroke")) ?: Color.Blue,
136136
strokeWidth = properties?.optDouble("stroke-width", 5.0)?.toFloat() ?: 5f,
137137
tag = properties,
138138
)
@@ -151,7 +151,7 @@ object GeoJsonParser {
151151
val polyline =
152152
Polyline(
153153
points = points,
154-
strokeColor = parseColor(properties?.optString("stroke")) ?: Color.BLUE,
154+
strokeColor = parseColor(properties?.optString("stroke")) ?: Color.Blue,
155155
strokeWidth = properties?.optDouble("stroke-width", 5.0)?.toFloat() ?: 5f,
156156
tag = properties,
157157
)
@@ -184,9 +184,9 @@ object GeoJsonParser {
184184
Polygon(
185185
points = outerRing,
186186
holes = holes,
187-
strokeColor = parseColor(properties?.optString("stroke")) ?: Color.BLACK,
187+
strokeColor = parseColor(properties?.optString("stroke")) ?: Color.Black,
188188
strokeWidth = properties?.optDouble("stroke-width", 3.0)?.toFloat() ?: 3f,
189-
fillColor = parseColor(properties?.optString("fill")) ?: Color.argb(100, 128, 128, 128),
189+
fillColor = parseColor(properties?.optString("fill")) ?: Color(red = 128, green = 128, blue = 128, alpha = 100),
190190
tag = properties,
191191
)
192192
return GeoJsonResult(polygons = listOf(polygon))
@@ -220,13 +220,9 @@ object GeoJsonParser {
220220
return points
221221
}
222222

223-
private fun parseColor(colorString: String?): Int? {
223+
private fun parseColor(colorString: String?): Color? {
224224
if (colorString == null) return null
225-
return try {
226-
Color.parseColor(colorString)
227-
} catch (e: IllegalArgumentException) {
228-
null
229-
}
225+
return colorString.toComposeColor()
230226
}
231227
}
232228

0 commit comments

Comments
 (0)