Skip to content

Commit 12d9034

Browse files
committed
fix(RouteViewer): fix route sort, color issues
fix #122
1 parent 906d922 commit 12d9034

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/components/viewers/route-viewer.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ function operatorIndexForRoute (operators, route) {
1818
else return 0
1919
}
2020

21+
/**
22+
* Determine the appropriate contrast color for text (white or black) based on
23+
* the input hex string (e.g., '#ff00ff') value.
24+
*
25+
* From https://stackoverflow.com/a/11868398/915811
26+
*
27+
* TODO: Move to @opentripplanner/core-utils once otp-rr uses otp-ui library.
28+
*/
29+
function getContrastYIQ (hexcolor) {
30+
hexcolor = hexcolor.replace('#', '')
31+
const r = parseInt(hexcolor.substr(0, 2), 16)
32+
const g = parseInt(hexcolor.substr(2, 2), 16)
33+
const b = parseInt(hexcolor.substr(4, 2), 16)
34+
const yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000
35+
return (yiq >= 128) ? '000000' : 'ffffff'
36+
}
37+
2138
class RouteViewer extends Component {
2239
static propTypes = {
2340
hideBackButton: PropTypes.bool,
@@ -110,8 +127,14 @@ class RouteRow extends PureComponent {
110127
render () {
111128
const {isActive, route, operator} = this.props
112129
const {defaultRouteColor, defaultRouteTextColor, longNameSplitter} = operator || {}
113-
const color = `#${defaultRouteTextColor || route.textColor || '000000'}`
114130
const backgroundColor = `#${defaultRouteColor || route.color || 'ffffff'}`
131+
// NOTE: text color is not a part of short response route object, so there
132+
// is no way to determine from OTP what the text color should be if the
133+
// background color is, say, black. Instead, determine the appropriate
134+
// contrast color and use that if no text color is available.
135+
const contrastColor = getContrastYIQ(backgroundColor)
136+
const color = `#${defaultRouteTextColor || route.textColor || contrastColor}`
137+
console.log(route, color, backgroundColor, contrastColor)
115138
// Default long name is empty string (long name is an optional GTFS value).
116139
let longName = ''
117140
if (route.longName) {
@@ -121,6 +144,9 @@ class RouteRow extends PureComponent {
121144
longName = (longNameSplitter && nameParts.length > 1)
122145
? nameParts[1]
123146
: route.longName
147+
// If long name and short name are identical, set long name to be an empty
148+
// string.
149+
if (longName === route.shortName) longName = ''
124150
}
125151
return (
126152
<div

lib/util/itinerary.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,21 @@ export function getLegBounds (leg) {
247247

248248
export function routeComparator (a, b) {
249249
let aComp, bComp
250-
if (a.sortOrder !== null && b.sortOrder !== null) {
250+
// If route sort order field is available, default sort to these predefined
251+
// values. NOTE: OTP/OBA defaults sort order values to -999, so instead of
252+
// checking that the sort order is not null, we just check that the values are
253+
// not equal. Technically, there could be cases where route_sort_order values
254+
// are equivalent, but ideally they should be unique.
255+
if (a.sortOrder !== b.sortOrder) {
251256
aComp = a.sortOrder
252257
bComp = b.sortOrder
253258
} else if (!isNaN(parseInt(a.shortName)) && !isNaN(parseInt(b.shortName))) {
259+
// Otherwise, if both short names can be parsed as integers, use these
260+
// numbers for sorting.
254261
aComp = parseInt(a.shortName)
255262
bComp = parseInt(b.shortName)
256263
} else {
264+
// Otherwise, default to short or long name strings.
257265
aComp = a.shortName || a.longName
258266
bComp = b.shortName || b.longName
259267
}

0 commit comments

Comments
 (0)