mirror of
https://github.com/chylex/IntelliJ-Rainbow-Brackets.git
synced 2025-05-10 00:34:04 +02:00
Refactoring: simplify random color API usage
This commit is contained in:
parent
5777d7bace
commit
fed9873be2
src/main/kotlin/com/github/izhangzhihao/rainbow/brackets
@ -1,6 +1,5 @@
|
||||
package com.github.izhangzhihao.rainbow.brackets
|
||||
|
||||
import com.github.izhangzhihao.rainbow.brackets.fromRGBstr
|
||||
import com.github.izhangzhihao.rainbow.brackets.settings.RainbowSettings
|
||||
import com.github.izhangzhihao.rainbow.brackets.util.memoize
|
||||
import com.intellij.codeInsight.daemon.impl.HighlightInfo
|
||||
@ -133,7 +132,7 @@ object RainbowHighlighter {
|
||||
}
|
||||
|
||||
private fun genByOption(option: String) =
|
||||
TextAttributes(fromRGBstr(randomColor(option)), null, null, null, 0)
|
||||
TextAttributes(randomColor(option), null, null, null, 0)
|
||||
|
||||
val memGetRainbowColorByLevel = { isDark: Boolean, rainbowName: String, level: Int -> generateColor(isDark, rainbowName, level) }.memoize()
|
||||
|
||||
|
@ -5,7 +5,7 @@ import com.github.izhangzhihao.rainbow.brackets.color.fromString
|
||||
import org.json.JSONObject
|
||||
import java.awt.Color
|
||||
|
||||
fun randomColor(options: String): String {
|
||||
fun randomColor(options: String): Color {
|
||||
val options = JSONObject(options)
|
||||
return com.github.izhangzhihao.rainbow.brackets.color.randomColor(
|
||||
fromString(options.getStringOrDefault("hue", "random")),
|
||||
@ -19,9 +19,4 @@ fun org.json.JSONObject.getStringOrDefault(key: String, default: String): String
|
||||
} catch (e: Exception) {
|
||||
default
|
||||
}
|
||||
}
|
||||
|
||||
fun fromRGBstr(str: String): Color {
|
||||
val split = str.trimStart('(').trimEnd(')').split(", ")
|
||||
return Color(split[0].toInt(), split[1].toInt(), split[2].toInt())
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package com.github.izhangzhihao.rainbow.brackets.color
|
||||
|
||||
enum class Format {
|
||||
HSL,
|
||||
RGB,
|
||||
HEX
|
||||
}
|
@ -15,9 +15,9 @@ fun Hue.getHueRange(): Pair<Int, Int> {
|
||||
}
|
||||
|
||||
fun fromString(str: String): Hue {
|
||||
return when (str) {
|
||||
"random" -> RandomHue
|
||||
is String -> ColorHue(Color.valueOf(str))
|
||||
else -> TODO()
|
||||
return when {
|
||||
str == "random" -> RandomHue
|
||||
str.startsWith("#") -> NumberHue(Integer.parseInt(str.replaceFirst("#", ""), 16))
|
||||
else -> ColorHue(Color.valueOf(str))
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,7 @@ import kotlin.random.Random
|
||||
fun randomColor(
|
||||
hue: Hue = RandomHue,
|
||||
luminosity: Luminosity = Luminosity.random,
|
||||
format: Format = Format.RGB
|
||||
): String {
|
||||
): java.awt.Color {
|
||||
|
||||
// First we pick a hue (H)
|
||||
val hueValue = pickHue(hue)
|
||||
@ -22,8 +21,7 @@ fun randomColor(
|
||||
// Then use S and H to determine brightness (B)
|
||||
val brightness = pickBrightness(hueValue, hue, saturation, luminosity)
|
||||
|
||||
// Then we return the HSB color in the desired format
|
||||
return setFormat(hueValue, saturation, brightness, format)
|
||||
return toColor(hueValue, saturation, brightness)
|
||||
}
|
||||
|
||||
private fun pickHue(hue: Hue): Int {
|
||||
@ -69,12 +67,9 @@ private fun pickBrightness(hueValue: Int, hue: Hue, saturation: Int, luminosity:
|
||||
}
|
||||
}
|
||||
|
||||
private fun setFormat(hueValue: Int, saturation: Int, brightness: Int, format: Format): String {
|
||||
return when (format) {
|
||||
Format.HSL -> TODO()
|
||||
Format.RGB -> HSVtoRGB(hueValue, saturation, brightness).toString()
|
||||
Format.HEX -> TODO()
|
||||
}
|
||||
private fun toColor(hueValue: Int, saturation: Int, brightness: Int): java.awt.Color {
|
||||
val rgb = HSVtoRGB(hueValue, saturation, brightness)
|
||||
return java.awt.Color(rgb.first, rgb.second, rgb.third)
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user