Skip to content

Commit

Permalink
Merge pull request #229 from SwEnt-Group8/feat/make-icon-change-color…
Browse files Browse the repository at this point in the history
…-in-function-of-nb-event

Feat/make icon change color in function of nb event
  • Loading branch information
kzepfl authored Dec 17, 2024
2 parents 8ff8c4a + 0f220cf commit 9928233
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
77 changes: 76 additions & 1 deletion app/src/main/java/com/android/streetworkapp/ui/map/MapUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import com.android.streetworkapp.ui.park.RatingComponent
import com.android.streetworkapp.ui.theme.ColorPalette
import com.android.streetworkapp.utils.LocationService
import com.android.streetworkapp.utils.PermissionManager
import com.google.android.gms.maps.model.BitmapDescriptor
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
Expand Down Expand Up @@ -106,6 +107,25 @@ fun MapScreen(
userViewModel.currentUser.value, initialLatLng.value, parkLocationViewModel.parks.value)
}

// Define how many event is considered a Hot place
val hotPlace = 5.0f

// Define the start and end colors for the gradient
val startColor = ColorPalette.LOGO_BLUE
val endColor = ColorPalette.LOGO_RED

// variable for each park color
var interpolatedColor: Color // set default color
var markerIcon: BitmapDescriptor

// Handling user MVVM
val currentUser = userViewModel.currentUser.collectAsState().value

if (currentUser != null) {
userViewModel.getParksByUid(currentUser.uid)
}

// Handle parks MVVM
val parks = parkLocationViewModel.parks.collectAsState().value

val parkList = parkViewModel.parkList.collectAsState()
Expand Down Expand Up @@ -145,13 +165,21 @@ fun MapScreen(
.forEach { park ->
++markerIndex

// define park location
val markerState =
rememberMarkerState(position = LatLng(park.location.lat, park.location.lon))

// Interpolate color based on number of event (make the gradient)
interpolatedColor =
gradientColor(startColor, endColor, (park.events.size) / hotPlace)

markerIcon = BitmapDescriptorFactory.defaultMarker(colorToHue(interpolatedColor))

// default marker for discovered park
MarkerInfoWindow(
tag = "Marker$markerIndex",
state = markerState,
icon = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE),
icon = markerIcon,
onClick = {
markerState.showInfoWindow()
if (selectedPark == park) {
Expand Down Expand Up @@ -222,3 +250,50 @@ fun MarkerInfoWindowContent(park: Park) {
}
}
}

/**
* Returns a color that is a gradient between two colors based on a fraction.
*
* @param startColor The starting color of the gradient
* @param endColor The ending color of the gradient
* @param fraction A value between 0 and 1 indicating the position between the start and end colors
* @return A Color that represents the gradient at the given fraction
*/
fun gradientColor(startColor: Color, endColor: Color, fraction: Float): Color {
val startR = startColor.red
val startG = startColor.green
val startB = startColor.blue
val startA = startColor.alpha

val endR = endColor.red
val endG = endColor.green
val endB = endColor.blue
val endA = endColor.alpha

val r = startR + fraction * (endR - startR)
val g = startG + fraction * (endG - startG)
val b = startB + fraction * (endB - startB)
val a = startA + fraction * (endA - startA)

return Color(r, g, b, a)
}

/**
* Converts a color to its hue value needed for BitMap
*
* @param color The color to be converted
* @return The hue value of the color, in degrees (0-360)
*/
fun colorToHue(color: Color): Float {
// Convert the Color to ARGB values (0-255)
val r = (color.red * 255).toInt()
val g = (color.green * 255).toInt()
val b = (color.blue * 255).toInt()

// Use the Android Color class to convert to HSV
val hsv = FloatArray(3)
android.graphics.Color.RGBToHSV(r, g, b, hsv)

// Return the hue value (0-360 degrees)
return hsv[0]
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/android/streetworkapp/ui/theme/Color.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ object ColorPalette {

val TUTORIAL_INTERACTION_1 = Color.Red
val TUTORIAL_INTERACTION_2 = Color.Green

// LOGO gradient
val LOGO_BLUE = Color(0xFF0228FF)
val LOGO_RED = Color(0xFFFD00FF)
}
45 changes: 45 additions & 0 deletions app/src/test/java/com/android/streetworkapp/ui/map/ColorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.android.streetworkapp.ui.map

import androidx.compose.ui.graphics.Color
import org.junit.Assert.assertEquals
import org.junit.Test

class ColorTest {
// test gradientColor
@Test
fun testGradientColorFraction0() {
val startColor = Color(1f, 0f, 0f, 1f) // Red color
val endColor = Color(0f, 0f, 1f, 1f) // Blue color
val result = gradientColor(startColor, endColor, 0f)

assertEquals(startColor, result)
}

@Test
fun testGradientColorFraction1() {
val startColor = Color(1f, 0f, 0f, 1f) // Red color
val endColor = Color(0f, 0f, 1f, 1f) // Blue color
val result = gradientColor(startColor, endColor, 1f)

assertEquals(endColor, result)
}

@Test
fun testGradientColorFraction05() {
val startColor = Color(1f, 0f, 0f, 1f) // Red color
val endColor = Color(0f, 0f, 1f, 1f) // Blue color
val expectedColor = Color(0.5f, 0f, 0.5f, 1f) // Expected midpoint color (purple)

val result = gradientColor(startColor, endColor, 0.5f)

assertEquals(expectedColor, result)
}
// test colorToHue
@Test
fun testColorToHueRed() {
val color = Color(1f, 0f, 0f, 1f) // Red color
val result = colorToHue(color)

assertEquals(0f, result, 0.1f) // Red has hue 0
}
}

0 comments on commit 9928233

Please sign in to comment.