Skip to content

Commit c530ee9

Browse files
committed
Merge pull request #8 from btmills/issue7
Expose color on Pattern object (fixes #7)
2 parents e0ba1f4 + 59c3bbf commit c530ee9

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ Returns a newly-generated, tiling SVG Pattern.
5656

5757
- `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.
5858

59+
#### Pattern.color
60+
61+
Gets the pattern's background color as a hexadecimal string.
62+
63+
```js
64+
GeoPattern.generate('GitHub').color // => "#455e8a"
65+
```
66+
5967
#### Pattern.toString() and Pattern.toSvg()
6068

6169
Gets the SVG string representing the pattern.

js/geopattern.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/color.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ function hex2rgb(hex) {
2424
} : null;
2525
}
2626

27+
/**
28+
* Converts an RGB color value to a hex string.
29+
* @param Object rgb RGB as r, g, and b keys
30+
* @return String Hex color string
31+
*/
32+
function rgb2hex(rgb) {
33+
return '#' + ['r', 'g', 'b'].map(function (key) {
34+
return rgb[key].toString(16);
35+
}).join('');
36+
}
37+
2738
/**
2839
* Converts an RGB color value to HSL. Conversion formula adapted from
2940
* http://en.wikipedia.org/wiki/HSL_color_space. This function adapted
@@ -100,6 +111,7 @@ function hsl2rgb(hsl) {
100111

101112
module.exports = {
102113
hex2rgb: hex2rgb,
114+
rgb2hex: rgb2hex,
103115
rgb2hsl: rgb2hsl,
104116
hsl2rgb: hsl2rgb,
105117
rgb2rgbString: function (rgb) {

lib/pattern.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Pattern.prototype.generateBackground = function () {
134134
rgb = color.hsl2rgb(baseColor);
135135
}
136136

137+
this.color = color.rgb2hex(rgb);
138+
137139
this.svg.rect(0, 0, '100%', '100%', {
138140
fill: color.rgb2rgbString(rgb)
139141
});

0 commit comments

Comments
 (0)