Skip to content

Commit

Permalink
Better custom color logic and updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
peacog committed Apr 25, 2024
1 parent d76ac95 commit eab2e89
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ const colors = {
};
```

Custom colors in hex or rgb can be used by providing a `color` option with the
desired color value. rgb, rgba, and hex values with or without alpha channel are all supported.
Custom colors can be used by providing a `color` option with the desired color value. A color string parser is included
to handle hex, rgb and other formats.
See [color-parse](https://github.com/colorjs/color-parse/tree/master#parsed-strings) for information about supported formats.

For example:
```js
// Add a GeoJSON layer with a custom inside a layer group called "Assets"
// Add a GeoJSON layer with a custom color inside a layer group called "Assets"
const opts = {
title: 'Animals',
url: '/farm/assets/geojson/animal/full',
Expand Down
11 changes: 9 additions & 2 deletions src/styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ const colors = {

// Returns an OpenLayers Style for a given color.
const colorStyles = (color) => {
color = color.startsWith('#') || color.startsWith('rgb') ? color : colors[color] || colors.yellow;
let strokeColor;
if (!color) {
strokeColor = colors.yellow;
} else if (colors[color]) {
strokeColor = colors[color];
} else {
strokeColor = color;
}

const stroke = new Stroke({
color: color,
color: strokeColor,
width: 2,
});
const fill = new Fill({
Expand Down

0 comments on commit eab2e89

Please sign in to comment.