Skip to content

Commit

Permalink
refactor(isochrones): changes colouring of isochrones to be a red to …
Browse files Browse the repository at this point in the history
…blue scale

Modified the way that the colouring of isochrone polygons is done to use a linear colour scale (red
to blue) rather than the pre-defined discrete colourings.
  • Loading branch information
rabidllama authored and TheGreatRefrigerator committed Apr 28, 2023
1 parent 0bc3530 commit 749c686
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ class IsochronesBuilder {
getPolygons = () => {
const polygons = []
if (this.responseData.features) {
// When you request multiple points in an isochrone request, ors returns them all as a single array
// of polygon features
const ranges = this.responseData.metadata.query.range
const maxRange = Math.max(ranges)

this.responseData.features.forEach((feature, index) => {
const polygon = { geometry: feature.geometry, properties: feature.properties }
polygon.properties.range_type = this.responseData.metadata.query.range_type

PolygonUtils.preparePolygonForView(polygon, this.translations, index)

PolygonUtils.preparePolygonForView(polygon, this.translations, index, maxRange)
polygons.push(polygon)
})
}
Expand Down
24 changes: 22 additions & 2 deletions src/support/polygon-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import htmlColors from 'html-colors'
import GeoUtils from '@/support/geo-utils'
import Leaflet from 'leaflet'
import store from '@/store/store'
import utils from '@/support/utils'

const PolygonUtils = {

Expand All @@ -10,10 +11,12 @@ const PolygonUtils = {
* @param {*} polygon
* @param {*} translations
* @param {*} index
* @param {number} maxRange
*/
preparePolygonForView (polygon, translations, index) {
preparePolygonForView: function (polygon, translations, index, maxRange) {
polygon.properties = polygon.properties || {}
polygon.properties.color = polygon.properties.color || PolygonUtils.buildPolygonColor(index)
// the "value" property contains the range for the feature
polygon.properties.color = polygon.properties.color || PolygonUtils.buildPolygonColorFromScale(polygon.properties.value, maxRange)
polygon.properties.fillColor = polygon.properties.fillColor || polygon.properties.color
polygon.properties.label = polygon.properties.label || PolygonUtils.buildPolygonLabel(polygon, translations)
polygon.properties.area = PolygonUtils.calcPolygonArea(polygon)
Expand Down Expand Up @@ -94,6 +97,23 @@ const PolygonUtils = {
const color = htmlColors.hex(names[index + 6])
return color
},
/**
* Generate a color (from red to blue) on a linear scale
* @param {Number} value The number to be placed on the scale
* @param {Number} scaleMax The maximum value of the scale
* @returns {String} color
*/
buildPolygonColorFromScale(value, scaleMax) {
let modifier = parseInt(255 * (value/scaleMax))

let red = modifier * 2
let blue = (255 - modifier) * 2
// Cap values to 255. By multiplying by two we ensure that we get a good middle color (where both are 255)
if (red > 255) red = 255
if (blue > 255) blue = 255

return utils.rgbDecimalToHex(red, 0, blue)
},
/**
* Build polygon label
* @param {Number} index
Expand Down
26 changes: 25 additions & 1 deletion src/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,31 @@ const Utils = {
const textColor = tinyColor2(backgroundColor).isLight() ? 'black' : 'white'
return textColor
},

/**
* Get a hexadecimal RGB representation of decimal (0-255) color values
* @param red
* @param green
* @param blue
* @returns {string}
*/
rgbDecimalToHex (red, green, blue) {
let r = this.decimalToHex(red)
let g = this.decimalToHex(green)
let b = this.decimalToHex(blue)
return '#'+r+g+b
},
/**
* Convert a decimal value to its hexadecimal representation
* @param decimal
* @returns {string}
*/
decimalToHex (decimal) {
let hex = Number(decimal).toString(16)
if (hex.length < 2) {
hex = '0' + hex
}
return hex
},
/**
* Blur the active element in order to close
* the mobile virtual keyboard if it is open
Expand Down

0 comments on commit 749c686

Please sign in to comment.