Skip to content

Commit

Permalink
Add a common Color implementation #5
Browse files Browse the repository at this point in the history
  • Loading branch information
HopeBaron committed Jul 8, 2020
1 parent 2307c5c commit 75ccafa
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 11 deletions.
27 changes: 27 additions & 0 deletions common/src/main/kotlin/com/gitlab/kordlib/common/Color.kt
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)
28 changes: 28 additions & 0 deletions common/src/test/kotlin/ColorTests.kt
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)


}
}
4 changes: 2 additions & 2 deletions core/src/main/kotlin/com/gitlab/kordlib/core/entity/Embed.kt
Original file line number Diff line number Diff line change
@@ -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 = """
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/com/gitlab/kordlib/core/entity/Role.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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
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(
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<GuildRoleCreateRequest>{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GuildRoleModifyRequest> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 75ccafa

Please sign in to comment.