Skip to content

Commit

Permalink
Merge pull request #8 from btmills/issue7
Browse files Browse the repository at this point in the history
Expose color on Pattern object (fixes #7)
  • Loading branch information
btmills committed Oct 30, 2014
2 parents e0ba1f4 + 59c3bbf commit c530ee9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ Returns a newly-generated, tiling SVG Pattern.

- `options.generator` Determines the pattern. [All of the original patterns](https://github.com/jasonlong/geo_pattern#available-patterns) are available in this port, and their names are camelCased.

#### Pattern.color

Gets the pattern's background color as a hexadecimal string.

```js
GeoPattern.generate('GitHub').color // => "#455e8a"
```

#### Pattern.toString() and Pattern.toSvg()

Gets the SVG string representing the pattern.
Expand Down
2 changes: 1 addition & 1 deletion js/geopattern.min.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions lib/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ function hex2rgb(hex) {
} : null;
}

/**
* Converts an RGB color value to a hex string.
* @param Object rgb RGB as r, g, and b keys
* @return String Hex color string
*/
function rgb2hex(rgb) {
return '#' + ['r', 'g', 'b'].map(function (key) {
return rgb[key].toString(16);
}).join('');
}

/**
* Converts an RGB color value to HSL. Conversion formula adapted from
* http://en.wikipedia.org/wiki/HSL_color_space. This function adapted
Expand Down Expand Up @@ -100,6 +111,7 @@ function hsl2rgb(hsl) {

module.exports = {
hex2rgb: hex2rgb,
rgb2hex: rgb2hex,
rgb2hsl: rgb2hsl,
hsl2rgb: hsl2rgb,
rgb2rgbString: function (rgb) {
Expand Down
2 changes: 2 additions & 0 deletions lib/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Pattern.prototype.generateBackground = function () {
rgb = color.hsl2rgb(baseColor);
}

this.color = color.rgb2hex(rgb);

this.svg.rect(0, 0, '100%', '100%', {
fill: color.rgb2rgbString(rgb)
});
Expand Down

0 comments on commit c530ee9

Please sign in to comment.