@@ -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+
2138class 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
0 commit comments