Initial commit
This commit is contained in:
54
src/commonMain/kotlin/nl/astraeus/css/Color.kt
Normal file
54
src/commonMain/kotlin/nl/astraeus/css/Color.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
package nl.astraeus.css
|
||||
|
||||
fun rgb(red: Int, green: Int, blue: Int): Color = RGBColor(red, green, blue)
|
||||
fun rgba(red: Int, green: Int, blue: Int, alpha: Double): Color = RGBAColor(red, green, blue, alpha)
|
||||
fun hsl(hue: Int, saturation: Int, lightness: Int): Color = HSLColor(hue, saturation, lightness)
|
||||
fun hsla(hue: Int, saturation: Int, lightness: Int, alpha: Double): Color = HSLAColor(hue, saturation, lightness, alpha)
|
||||
|
||||
open class Color : CssProperty() {
|
||||
|
||||
override fun css(): String = "#xxxxxx"
|
||||
|
||||
}
|
||||
|
||||
class RGBColor(
|
||||
val red: Int,
|
||||
val green: Int,
|
||||
val blue: Int
|
||||
) : Color() {
|
||||
|
||||
override fun css(): String = "rgb($red, $green, $blue)"
|
||||
|
||||
}
|
||||
|
||||
class RGBAColor(
|
||||
val red: Int,
|
||||
val green: Int,
|
||||
val blue: Int,
|
||||
val alpha: Double
|
||||
) : Color() {
|
||||
|
||||
override fun css(): String = "rgba($red, $green, $blue, $alpha)"
|
||||
|
||||
}
|
||||
|
||||
class HSLColor(
|
||||
val hue: Int,
|
||||
val saturation: Int,
|
||||
val lightness: Int
|
||||
) : Color() {
|
||||
|
||||
override fun css(): String = "hsl($hue, $saturation, $lightness)"
|
||||
|
||||
}
|
||||
|
||||
class HSLAColor(
|
||||
val hue: Int,
|
||||
val saturation: Int,
|
||||
val lightness: Int,
|
||||
val alpha: Double
|
||||
) : Color() {
|
||||
|
||||
override fun css(): String = "hsla($hue, $saturation, $lightness, $alpha)"
|
||||
|
||||
}
|
||||
108
src/commonMain/kotlin/nl/astraeus/css/CssBuilder.kt
Normal file
108
src/commonMain/kotlin/nl/astraeus/css/CssBuilder.kt
Normal file
@@ -0,0 +1,108 @@
|
||||
package nl.astraeus.css
|
||||
|
||||
@DslMarker
|
||||
annotation class CssTagMarker
|
||||
|
||||
@CssTagMarker
|
||||
open class Style(
|
||||
var color: Color? = null,
|
||||
var backgroundColor: Color? = null,
|
||||
var left: Measurement? = null,
|
||||
var top: Measurement? = null,
|
||||
var width: Measurement? = null,
|
||||
var height: Measurement? = null,
|
||||
var fontFamily: PlainProperty? = null,
|
||||
var fontSize: FontSize? = null
|
||||
) {
|
||||
|
||||
fun getMapping() = mapOf(
|
||||
"color" to color,
|
||||
"background-color" to backgroundColor,
|
||||
"left" to left,
|
||||
"top" to top,
|
||||
"width" to width,
|
||||
"height" to height,
|
||||
"font-family" to fontFamily,
|
||||
"font-size" to fontSize
|
||||
)
|
||||
|
||||
fun propertyCss(indent: String, name: String, prop: CssProperty?): String = if (prop != null) {
|
||||
"$indent$name: ${prop.css()};\n"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
|
||||
fun generatePropertyCss(indent: String): String {
|
||||
val builder = StringBuilder()
|
||||
|
||||
for ((name, prop) in getMapping()) {
|
||||
builder.append(propertyCss(indent, name, prop))
|
||||
}
|
||||
|
||||
return builder.toString()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@CssTagMarker
|
||||
open class StyleDefinition : Style() {
|
||||
val definitions: MutableMap<String, StyleDefinition> = mutableMapOf()
|
||||
val includes: MutableList<Style> = mutableListOf()
|
||||
|
||||
fun css(selector: String, style: StyleDefinition.() -> Unit) {
|
||||
val styleValue = StyleDefinition()
|
||||
|
||||
style(styleValue)
|
||||
|
||||
definitions[selector] = styleValue
|
||||
}
|
||||
|
||||
fun include(style: Style) {
|
||||
includes.add(style)
|
||||
}
|
||||
|
||||
open fun generateCss(namespace: String = "", indent: String = " "): String {
|
||||
val builder = StringBuilder()
|
||||
|
||||
for ((name, prop) in definitions) {
|
||||
val css = StringBuilder()
|
||||
css.append(prop.generatePropertyCss(indent))
|
||||
for (style in prop.includes) {
|
||||
css.append(style.generatePropertyCss(indent))
|
||||
}
|
||||
if (css.isNotBlank()) {
|
||||
builder.append("$namespace $name".trim())
|
||||
builder.append(" {\n")
|
||||
builder.append(css)
|
||||
builder.append("}\n\n")
|
||||
}
|
||||
|
||||
builder.append(prop.generateCss( "${namespace} $name".trim(), indent))
|
||||
}
|
||||
|
||||
return builder.toString()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun css(definition: Style.() -> Unit): Style {
|
||||
val css = Style()
|
||||
|
||||
definition(css)
|
||||
|
||||
return css
|
||||
}
|
||||
|
||||
class CssBuilder {
|
||||
var definition: StyleDefinition = StyleDefinition()
|
||||
|
||||
fun style(definition: StyleDefinition.() -> Unit) {
|
||||
definition(this.definition)
|
||||
}
|
||||
|
||||
fun getCss(): String = definition.generateCss()
|
||||
|
||||
override fun toString(): String {
|
||||
return "CssBuilder(${definition.generateCss()})"
|
||||
}
|
||||
}
|
||||
17
src/commonMain/kotlin/nl/astraeus/css/CssProperty.kt
Normal file
17
src/commonMain/kotlin/nl/astraeus/css/CssProperty.kt
Normal file
@@ -0,0 +1,17 @@
|
||||
package nl.astraeus.css
|
||||
|
||||
abstract class CssProperty {
|
||||
|
||||
abstract fun css(): String
|
||||
|
||||
}
|
||||
|
||||
fun plain(value: String) = PlainProperty(value)
|
||||
|
||||
class PlainProperty(
|
||||
val value: String
|
||||
): CssProperty() {
|
||||
|
||||
override fun css(): String = value
|
||||
|
||||
}
|
||||
54
src/commonMain/kotlin/nl/astraeus/css/Measurement.kt
Normal file
54
src/commonMain/kotlin/nl/astraeus/css/Measurement.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
package nl.astraeus.css
|
||||
|
||||
fun px(nr: Int): Measurement = Measurement(MeasurementType.PX, nr)
|
||||
fun em(nr: Int): Measurement = Measurement(MeasurementType.EM, nr)
|
||||
fun perc(nr: Int): Measurement = Measurement(MeasurementType.PERCENTAGE, nr)
|
||||
|
||||
enum class MeasurementType {
|
||||
PX,
|
||||
EM,
|
||||
PERCENTAGE,
|
||||
OTHER
|
||||
}
|
||||
|
||||
open class Measurement(
|
||||
val type: MeasurementType,
|
||||
val intValue: Int = 0,
|
||||
val stringValue: String = ""
|
||||
) : CssProperty() {
|
||||
|
||||
override fun css(): String = when(type) {
|
||||
MeasurementType.PX -> {
|
||||
"${intValue}px"
|
||||
}
|
||||
MeasurementType.EM -> {
|
||||
"${intValue}em"
|
||||
}
|
||||
MeasurementType.PERCENTAGE -> {
|
||||
"${stringValue}%"
|
||||
}
|
||||
else -> {
|
||||
error("Unhandled type $type")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum class FontSizeType {
|
||||
PX,
|
||||
EM,
|
||||
PERCENTAGE,
|
||||
|
||||
}
|
||||
|
||||
class FontSize(
|
||||
val fontType: FontSizeType,
|
||||
intValue: Int = 0,
|
||||
stringValue: String = ""
|
||||
) : Measurement(
|
||||
MeasurementType.PX,
|
||||
intValue,
|
||||
stringValue
|
||||
) {
|
||||
override fun css(): String = "12px"
|
||||
}
|
||||
Reference in New Issue
Block a user