-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a common Color implementation #5
- Loading branch information
Showing
8 changed files
with
66 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.gitlab.kordlib.common | ||
|
||
|
||
class Color(val rgb: Int) { | ||
constructor(red: Int, green: Int, blue: Int) : this(rgb(red, green, blue)) | ||
|
||
val red: Int get() = (rgb shr 16) and 0xFF | ||
val green: Int get() = (rgb shr 8) and 0xFF | ||
val blue: Int get() = (rgb shr 0) and 0xFF | ||
|
||
init { | ||
require(rgb in 0..0xFFFFFF) { "RGB should be in range of 0..16777215 but was $rgb" } | ||
} | ||
} | ||
|
||
private fun rgb(red: Int, green: Int, blue: Int): Int { | ||
require(red in 0..255) { "Red should be in range of 0..255 but was $red" } | ||
require(green in 0..255) { "Green should be in range of 0..255 but was $green" } | ||
require(blue in 0..255) { "Blue should be in range of 0..255 but was $blue" } | ||
|
||
|
||
return red and 0xFF shl 16 or | ||
(green and 0xFF shl 8) or | ||
(blue and 0xFF) shl 0 | ||
} | ||
|
||
val java.awt.Color.kColor get() = Color(rgb) |
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,28 @@ | ||
import com.gitlab.kordlib.common.Color | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import kotlin.test.assertEquals | ||
|
||
class ColorTests { | ||
@Test | ||
fun `Color throws if invalid rgb value is provided`() { | ||
assertThrows<IllegalArgumentException> { Color(-1) } | ||
assertThrows<IllegalArgumentException> { Color(256, 256, 300) } | ||
} | ||
|
||
@Test | ||
fun `Color provides a correct value`() { | ||
val red = Color(0xFF0000) | ||
assertEquals(255, red.red) | ||
assertEquals(0, red.green) | ||
assertEquals(0, red.blue) | ||
|
||
val white = Color(255, 255, 255) | ||
assertEquals(255, white.red) | ||
assertEquals(255, white.green) | ||
assertEquals(255, white.blue) | ||
assertEquals(0xFFFFFF, white.rgb) | ||
|
||
|
||
} | ||
} |
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
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
2 changes: 1 addition & 1 deletion
2
rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/message/EmbedBuilder.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
4 changes: 2 additions & 2 deletions
4
rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/role/RoleCreateBuilder.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
4 changes: 2 additions & 2 deletions
4
rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/role/RoleModifyBuilder.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
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