Skip to content

Commit

Permalink
Fixed color parsing for color names.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamaskozmer committed Jun 21, 2022
1 parent 2719fb4 commit 2a1fd24
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.instructure.pandautils.utils

import android.content.Context
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import androidx.annotation.ColorInt
Expand Down Expand Up @@ -135,11 +134,21 @@ object ColorKeeper : PrefManager(PREFERENCE_FILE_NAME) {
* @return The parsed color, or [defaultColor] if the string could not be parsed
*/
private fun parseColor(hexColor: String): Int = try {
ColorUtils.parseColor("#${hexColor.trimMargin("#")}", defaultColor = defaultColor)
val trimmedColorCode = getTrimmedColorCode(hexColor)
ColorUtils.parseColor(trimmedColorCode, defaultColor = defaultColor)
} catch (e: IllegalArgumentException) {
defaultColor
}

// There might be cases where the color code from the response contains whitespaces.
private fun getTrimmedColorCode(colorCode: String): String {
return if (colorCode.contains("#")) {
"#${colorCode.trimMargin("#")}"
} else {
colorCode
}
}

/**
* Generates a generic color based on the canvas context id, this will produce consistent colors for a given course
* @param canvasContext a valid canvas context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,20 @@ object ThemePrefs : PrefManager("CanvasTheme") {

private fun parseColor(hexColor: String, defaultColor: Int): Int {
try {
return ColorUtils.parseColor("#${hexColor.trimMargin("#")}", defaultColor = defaultColor)
val trimmedColorCode = getTrimmedColorCode(hexColor)
return ColorUtils.parseColor(trimmedColorCode, defaultColor = defaultColor)
} catch (e: IllegalArgumentException) {
return defaultColor
}
}

// There might be cases where the color codes from the response contain whitespaces.
private fun getTrimmedColorCode(colorCode: String): String {
return if (colorCode.contains("#")) {
"#${colorCode.trimMargin("#")}"
} else {
colorCode
}
}

}

0 comments on commit 2a1fd24

Please sign in to comment.