mirror of
https://github.com/chylex/IntelliJ-Rainbow-Brackets.git
synced 2025-05-30 22:34:06 +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
|
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.settings.RainbowSettings
|
||||||
import com.github.izhangzhihao.rainbow.brackets.util.memoize
|
import com.github.izhangzhihao.rainbow.brackets.util.memoize
|
||||||
import com.intellij.codeInsight.daemon.impl.HighlightInfo
|
import com.intellij.codeInsight.daemon.impl.HighlightInfo
|
||||||
@ -133,7 +132,7 @@ object RainbowHighlighter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun genByOption(option: String) =
|
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()
|
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 org.json.JSONObject
|
||||||
import java.awt.Color
|
import java.awt.Color
|
||||||
|
|
||||||
fun randomColor(options: String): String {
|
fun randomColor(options: String): Color {
|
||||||
val options = JSONObject(options)
|
val options = JSONObject(options)
|
||||||
return com.github.izhangzhihao.rainbow.brackets.color.randomColor(
|
return com.github.izhangzhihao.rainbow.brackets.color.randomColor(
|
||||||
fromString(options.getStringOrDefault("hue", "random")),
|
fromString(options.getStringOrDefault("hue", "random")),
|
||||||
@ -19,9 +19,4 @@ fun org.json.JSONObject.getStringOrDefault(key: String, default: String): String
|
|||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
default
|
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 {
|
fun fromString(str: String): Hue {
|
||||||
return when (str) {
|
return when {
|
||||||
"random" -> RandomHue
|
str == "random" -> RandomHue
|
||||||
is String -> ColorHue(Color.valueOf(str))
|
str.startsWith("#") -> NumberHue(Integer.parseInt(str.replaceFirst("#", ""), 16))
|
||||||
else -> TODO()
|
else -> ColorHue(Color.valueOf(str))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,7 @@ import kotlin.random.Random
|
|||||||
fun randomColor(
|
fun randomColor(
|
||||||
hue: Hue = RandomHue,
|
hue: Hue = RandomHue,
|
||||||
luminosity: Luminosity = Luminosity.random,
|
luminosity: Luminosity = Luminosity.random,
|
||||||
format: Format = Format.RGB
|
): java.awt.Color {
|
||||||
): String {
|
|
||||||
|
|
||||||
// First we pick a hue (H)
|
// First we pick a hue (H)
|
||||||
val hueValue = pickHue(hue)
|
val hueValue = pickHue(hue)
|
||||||
@ -22,8 +21,7 @@ fun randomColor(
|
|||||||
// Then use S and H to determine brightness (B)
|
// Then use S and H to determine brightness (B)
|
||||||
val brightness = pickBrightness(hueValue, hue, saturation, luminosity)
|
val brightness = pickBrightness(hueValue, hue, saturation, luminosity)
|
||||||
|
|
||||||
// Then we return the HSB color in the desired format
|
return toColor(hueValue, saturation, brightness)
|
||||||
return setFormat(hueValue, saturation, brightness, format)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun pickHue(hue: Hue): Int {
|
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 {
|
private fun toColor(hueValue: Int, saturation: Int, brightness: Int): java.awt.Color {
|
||||||
return when (format) {
|
val rgb = HSVtoRGB(hueValue, saturation, brightness)
|
||||||
Format.HSL -> TODO()
|
return java.awt.Color(rgb.first, rgb.second, rgb.third)
|
||||||
Format.RGB -> HSVtoRGB(hueValue, saturation, brightness).toString()
|
|
||||||
Format.HEX -> TODO()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user