Skip to content

Commit

Permalink
Added ability to use 0x numbers (0xabcdef for example) as colors
Browse files Browse the repository at this point in the history
  • Loading branch information
adamldavis committed Jul 20, 2017
1 parent 560d9bc commit e61e0b7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/main/groovy/org/groocss/Color.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/org/groocss/GrooCSS.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
24 changes: 14 additions & 10 deletions src/main/groovy/org/groocss/StyleGroup.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -1215,5 +1215,9 @@ class StyleGroup extends Selectable {
x
}

private String handleColor(value) {
(value instanceof Number) ? "${new Color((Number) value)}" : "$value"
}

}

11 changes: 11 additions & 0 deletions src/test/groovy/org/groocss/GroocssSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)} }
}

}

0 comments on commit e61e0b7

Please sign in to comment.