-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Issue #7 hex colors
- Loading branch information
Showing
2 changed files
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
graphics/src/commonMain/kotlin/dev/icerock/moko/graphics/ColorHEX.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.icerock.moko.graphics | ||
|
||
fun Color.Companion.parseColor(colorHEX: String): Color { | ||
if (colorHEX[0] != '#') throw IllegalArgumentException("Unknown color") | ||
var ARGB = colorHEX.substring(1).toLong(16) | ||
if (colorHEX.length == 7) { | ||
ARGB = ARGB or 0x00000000ff000000 | ||
} else if (colorHEX.length != 9) { | ||
throw IllegalArgumentException("Unknown color") | ||
} | ||
return Color( | ||
alpha = (ARGB.shr(24) and 0xFF).toInt(), | ||
red = (ARGB.shr(16) and 0xFF).toInt(), | ||
green = (ARGB.shr(8) and 0xFF).toInt(), | ||
blue = (ARGB.shr(0) and 0xFF).toInt(), | ||
) | ||
} |