From e61e0b75cefd2aaa30ef1c2ea488f4477a68b52b Mon Sep 17 00:00:00 2001 From: Adam Davis Date: Thu, 20 Jul 2017 15:25:17 -0400 Subject: [PATCH] Added ability to use 0x numbers (0xabcdef for example) as colors --- src/main/groovy/org/groocss/Color.groovy | 6 +++++ src/main/groovy/org/groocss/GrooCSS.groovy | 3 +++ src/main/groovy/org/groocss/StyleGroup.groovy | 24 +++++++++++-------- .../groovy/org/groocss/GroocssSpec.groovy | 11 +++++++++ 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/main/groovy/org/groocss/Color.groovy b/src/main/groovy/org/groocss/Color.groovy index 8201296..4e98f4e 100644 --- a/src/main/groovy/org/groocss/Color.groovy +++ b/src/main/groovy/org/groocss/Color.groovy @@ -10,6 +10,12 @@ import java.math.MathContext @EqualsAndHashCode class Color { + Color(Number num) { + int r = (num / 0x10000).toInteger() % 256i + int g = (num / 0x100).toInteger() % 256i + int b = (num).toInteger() % 256i + color = new java.awt.Color(r, g, b) + } Color(String name, String colorStr) { this.name = name setColor(colorStr) diff --git a/src/main/groovy/org/groocss/GrooCSS.groovy b/src/main/groovy/org/groocss/GrooCSS.groovy index b36403b..fad7cc8 100644 --- a/src/main/groovy/org/groocss/GrooCSS.groovy +++ b/src/main/groovy/org/groocss/GrooCSS.groovy @@ -244,6 +244,9 @@ class GrooCSS extends Script { Color c(String colorStr) { new Color(colorStr) } + Color clr(Number num) {c(num)} + Color c(Number num) { new Color(num) } + /** Creates a new {@link org.groocss.Color} object with a name. */ Color c(String name, String colorStr) { new Color(name, colorStr) } diff --git a/src/main/groovy/org/groocss/StyleGroup.groovy b/src/main/groovy/org/groocss/StyleGroup.groovy index f19a298..68c4985 100644 --- a/src/main/groovy/org/groocss/StyleGroup.groovy +++ b/src/main/groovy/org/groocss/StyleGroup.groovy @@ -224,7 +224,7 @@ class StyleGroup extends Selectable { } /** Sets the background-color of an element */ StyleGroup backgroundColor (value) { - styles << new Style(name: 'backgroundColor', value: "$value") + styles << new Style(name: 'backgroundColor', value: handleColor(value)) this } /** Sets the background-image for an element */ @@ -274,7 +274,7 @@ class StyleGroup extends Selectable { } /** Sets the color of the bottom border */ StyleGroup borderBottomColor (value) { - styles << new Style(name: 'borderBottomColor', value: "$value") + styles << new Style(name: 'borderBottomColor', value: handleColor(value)) this } /** Sets the shape of the border of the bottom-left corner */ @@ -304,7 +304,7 @@ class StyleGroup extends Selectable { } /** Sets the color of an element's border (can have up to four values) */ StyleGroup borderColor (value) { - styles << new Style(name: 'borderColor', value: "$value") + styles << new Style(name: 'borderColor', value: handleColor(value)) this } /** A shorthand property for setting or returning all the borderImage* properties */ @@ -344,7 +344,7 @@ class StyleGroup extends Selectable { } /** Sets the color of the left border */ StyleGroup borderLeftColor (value) { - styles << new Style(name: 'borderLeftColor', value: "$value") + styles << new Style(name: 'borderLeftColor', value: handleColor(value)) this } /** Sets the style of the left border */ @@ -372,7 +372,7 @@ class StyleGroup extends Selectable { } /** Sets the color of the right border */ StyleGroup borderRightColor (value) { - styles << new Style(name: 'borderRightColor', value: "$value") + styles << new Style(name: 'borderRightColor', value: handleColor(value)) this } /** Sets the style of the right border */ @@ -402,7 +402,7 @@ class StyleGroup extends Selectable { } /** Sets the color of the top border */ StyleGroup borderTopColor (value) { - styles << new Style(name: 'borderTopColor', value: "$value") + styles << new Style(name: 'borderTopColor', value: handleColor(value)) this } /** Sets the shape of the border of the top-left corner */ @@ -470,7 +470,7 @@ class StyleGroup extends Selectable { } /** Sets the color of the text */ StyleGroup color (value) { - styles << new Style(name: 'color', value: "$value") + styles << new Style(name: 'color', value: handleColor(value)) this } /** Sets the number of columns an element should be divided into */ @@ -495,7 +495,7 @@ class StyleGroup extends Selectable { } /** Sets the color of the rule between columns */ StyleGroup columnRuleColor (value) { - styles << new Style(name: 'columnRuleColor', value: "$value") + styles << new Style(name: 'columnRuleColor', value: handleColor(value)) this } /** Sets the style of the rule between columns */ @@ -808,7 +808,7 @@ class StyleGroup extends Selectable { } /** Sets the color of the outline around a element */ StyleGroup outlineColor (value) { - styles << new Style(name: 'outlineColor', value: "$value") + styles << new Style(name: 'outlineColor', value: handleColor(value)) this } /** Offsets an outline, and draws it beyond the border edge */ @@ -938,7 +938,7 @@ class StyleGroup extends Selectable { } /** Sets the color of the text-decoration */ StyleGroup textDecorationColor (value) { - styles << new Style(name: 'textDecorationColor', value: "$value") + styles << new Style(name: 'textDecorationColor', value: handleColor(value)) this } /** Sets the type of line in a text-decoration */ @@ -1215,5 +1215,9 @@ class StyleGroup extends Selectable { x } + private String handleColor(value) { + (value instanceof Number) ? "${new Color((Number) value)}" : "$value" + } + } diff --git a/src/test/groovy/org/groocss/GroocssSpec.groovy b/src/test/groovy/org/groocss/GroocssSpec.groovy index 6f3b645..8f64cd4 100644 --- a/src/test/groovy/org/groocss/GroocssSpec.groovy +++ b/src/test/groovy/org/groocss/GroocssSpec.groovy @@ -1073,4 +1073,15 @@ class GroocssSpec extends Specification { "div.date-time{color: #123;}" | { def dt = div.date_time; sg(dt) {color '#123'} } } + def "should use 0x numbers as colors"() { + expect: + assert "${GrooCSS.withConfig { convertUnderline() }.process closure}" == css + where: + css | closure + "a{color: #cafeee;}" | { a { color 0xcafeee} } + "a{color: #eeeeee;}" | { a { color 0xEEEEEE} } + "a{color: #cafeee;}" | { a { color c(0xcafeee)} } + "a{color: #eeeeee;}" | { a { color c(0xEEEEEE)} } + } + }