From 75ccafac10eeaeb74180466755a66913078d4a8b Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Wed, 17 Jun 2020 11:56:33 +0300 Subject: [PATCH] Add a common Color implementation #5 --- .../kotlin/com/gitlab/kordlib/common/Color.kt | 27 ++++++++++++++++++ common/src/test/kotlin/ColorTests.kt | 28 +++++++++++++++++++ .../com/gitlab/kordlib/core/entity/Embed.kt | 4 +-- .../com/gitlab/kordlib/core/entity/Role.kt | 4 +-- .../rest/builder/message/EmbedBuilder.kt | 2 +- .../rest/builder/role/RoleCreateBuilder.kt | 4 +-- .../rest/builder/role/RoleModifyBuilder.kt | 4 +-- .../kordlib/rest/services/RestServiceTest.kt | 4 +-- 8 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 common/src/main/kotlin/com/gitlab/kordlib/common/Color.kt create mode 100644 common/src/test/kotlin/ColorTests.kt diff --git a/common/src/main/kotlin/com/gitlab/kordlib/common/Color.kt b/common/src/main/kotlin/com/gitlab/kordlib/common/Color.kt new file mode 100644 index 000000000000..3437dd4ba9af --- /dev/null +++ b/common/src/main/kotlin/com/gitlab/kordlib/common/Color.kt @@ -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) \ No newline at end of file diff --git a/common/src/test/kotlin/ColorTests.kt b/common/src/test/kotlin/ColorTests.kt new file mode 100644 index 000000000000..4b9a265adafd --- /dev/null +++ b/common/src/test/kotlin/ColorTests.kt @@ -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 { Color(-1) } + assertThrows { 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) + + + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/entity/Embed.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/entity/Embed.kt index ae08cc382c52..e53a9c954347 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/entity/Embed.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/entity/Embed.kt @@ -1,11 +1,11 @@ package com.gitlab.kordlib.core.entity +import com.gitlab.kordlib.common.Color import com.gitlab.kordlib.core.Kord import com.gitlab.kordlib.core.KordObject import com.gitlab.kordlib.core.cache.data.* import com.gitlab.kordlib.core.toInstant import com.gitlab.kordlib.rest.builder.message.EmbedBuilder -import java.awt.Color import java.time.Instant internal const val embedDeprecationMessage = """ @@ -49,7 +49,7 @@ data class Embed(val data: EmbedData, override val kord: Kord) : KordObject { /** * The color of the embed, if present. */ - val color: Color? get() = data.color?.let { Color(it, true) } + val color: Color? get() = data.color?.let { Color(it) } /** * The footer, if present. diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/entity/Role.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/entity/Role.kt index 0715236c5488..2c2dd5f9a405 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/entity/Role.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/entity/Role.kt @@ -1,5 +1,6 @@ package com.gitlab.kordlib.core.entity +import com.gitlab.kordlib.common.Color import com.gitlab.kordlib.common.entity.Permissions import com.gitlab.kordlib.common.entity.Snowflake import com.gitlab.kordlib.core.Kord @@ -7,7 +8,6 @@ import com.gitlab.kordlib.core.behavior.RoleBehavior import com.gitlab.kordlib.core.cache.data.RoleData import com.gitlab.kordlib.core.supplier.EntitySupplier import com.gitlab.kordlib.core.supplier.EntitySupplyStrategy -import java.awt.Color import java.util.* data class Role( @@ -22,7 +22,7 @@ data class Role( override val guildId: Snowflake get() = Snowflake(data.guildId) - val color: Color get() = Color(data.color, true) + val color: Color get() = Color(data.color) val hoisted: Boolean get() = data.hoisted diff --git a/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/message/EmbedBuilder.kt b/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/message/EmbedBuilder.kt index ce8de219bd91..9d4ec0d42456 100644 --- a/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/message/EmbedBuilder.kt +++ b/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/message/EmbedBuilder.kt @@ -1,9 +1,9 @@ package com.gitlab.kordlib.rest.builder.message +import com.gitlab.kordlib.common.Color import com.gitlab.kordlib.common.annotation.KordDsl import com.gitlab.kordlib.rest.builder.RequestBuilder import com.gitlab.kordlib.rest.json.request.* -import java.awt.Color import java.time.Instant import java.time.format.DateTimeFormatter diff --git a/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/role/RoleCreateBuilder.kt b/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/role/RoleCreateBuilder.kt index ce2705de6fbe..f5bf30caa2fc 100644 --- a/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/role/RoleCreateBuilder.kt +++ b/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/role/RoleCreateBuilder.kt @@ -1,10 +1,10 @@ package com.gitlab.kordlib.rest.builder.role +import com.gitlab.kordlib.common.Color +import com.gitlab.kordlib.common.annotation.KordDsl import com.gitlab.kordlib.common.entity.Permissions import com.gitlab.kordlib.rest.builder.AuditRequestBuilder -import com.gitlab.kordlib.common.annotation.KordDsl import com.gitlab.kordlib.rest.json.request.GuildRoleCreateRequest -import java.awt.Color @KordDsl class RoleCreateBuilder : AuditRequestBuilder{ diff --git a/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/role/RoleModifyBuilder.kt b/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/role/RoleModifyBuilder.kt index 7280c3113238..c371c6ebe4ae 100644 --- a/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/role/RoleModifyBuilder.kt +++ b/rest/src/main/kotlin/com/gitlab/kordlib/rest/builder/role/RoleModifyBuilder.kt @@ -1,10 +1,10 @@ package com.gitlab.kordlib.rest.builder.role +import com.gitlab.kordlib.common.Color +import com.gitlab.kordlib.common.annotation.KordDsl import com.gitlab.kordlib.common.entity.Permissions import com.gitlab.kordlib.rest.builder.AuditRequestBuilder -import com.gitlab.kordlib.common.annotation.KordDsl import com.gitlab.kordlib.rest.json.request.GuildRoleModifyRequest -import java.awt.Color @KordDsl class RoleModifyBuilder : AuditRequestBuilder { diff --git a/rest/src/test/kotlin/com/gitlab/kordlib/rest/services/RestServiceTest.kt b/rest/src/test/kotlin/com/gitlab/kordlib/rest/services/RestServiceTest.kt index b60472403653..d2b3a99747a0 100644 --- a/rest/src/test/kotlin/com/gitlab/kordlib/rest/services/RestServiceTest.kt +++ b/rest/src/test/kotlin/com/gitlab/kordlib/rest/services/RestServiceTest.kt @@ -1,5 +1,6 @@ package com.gitlab.kordlib.rest.services +import com.gitlab.kordlib.common.Color import com.gitlab.kordlib.common.entity.* import com.gitlab.kordlib.rest.Image import com.gitlab.kordlib.rest.json.request.* @@ -11,7 +12,6 @@ import io.ktor.client.HttpClient import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.* import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable -import java.awt.Color import java.util.* fun image(path: String): String { @@ -264,7 +264,7 @@ class RestServiceTest { val role = createGuildRole(guildId) { name = "Sudoers" permissions = Permissions { +Permission.Administrator } - color = Color.RED + color = Color(0xFF0000) hoist = true mentionable = true }