This repository has been archived by the owner on Mar 3, 2025. It is now read-only.
forked from HSLdevcom/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
172 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
client-next/src/components/ItineraryList/ItineraryHeaderLegContent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Leg } from '../../gql/graphql.ts'; | ||
import { useMemo } from 'react'; | ||
import { getColorForMode } from '../../util/getColorForMode.ts'; | ||
import { isTransitMode } from '../../util/isTransitMode.ts'; | ||
import { generateTextColor } from '../../util/generateTextColor.ts'; | ||
|
||
export function ItineraryHeaderLegContent({ | ||
leg, | ||
earliestStartTime, | ||
maxSpan, | ||
startPx, | ||
pxSpan, | ||
}: { | ||
leg: Leg; | ||
earliestStartTime: string | null; | ||
maxSpan: number; | ||
startPx: number; | ||
pxSpan: number; | ||
}) { | ||
const startPct = useMemo( | ||
() => (new Date(leg.expectedStartTime).getTime() - new Date(earliestStartTime!).getTime()) / maxSpan, | ||
[leg.expectedStartTime, earliestStartTime, maxSpan], | ||
); | ||
|
||
const widthPx = useMemo( | ||
() => | ||
(pxSpan * (new Date(leg.expectedEndTime).getTime() - new Date(leg.expectedStartTime).getTime())) / maxSpan - 1, | ||
[pxSpan, leg, maxSpan], | ||
); | ||
|
||
const leftPx = startPx + startPct * pxSpan + 1; | ||
|
||
const showPublicCode = | ||
widthPx > 40 && isTransitMode(leg.mode) && leg.line?.publicCode && leg.line.publicCode.length <= 6; | ||
|
||
const modeColor = getColorForMode(leg.mode); | ||
const legTextColor = useMemo(() => generateTextColor(modeColor), [modeColor]); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
position: 'absolute', | ||
width: `${widthPx}px`, | ||
height: '22px', | ||
left: leftPx, | ||
color: legTextColor, | ||
background: modeColor, | ||
fontWeight: 'bold', | ||
textShadow: 'none', | ||
marginTop: '-1px', | ||
paddingTop: '3px', | ||
textAlign: 'center', | ||
overflow: 'hidden', | ||
}} | ||
> | ||
<div | ||
style={{ | ||
background: legTextColor, | ||
maskImage: `url(/img/mode/${leg.mode.toLowerCase()}.png)`, | ||
maskRepeat: 'no-repeat', | ||
WebkitMaskImage: `url(/img/mode/${leg.mode.toLowerCase()}.png)`, | ||
WebkitMaskRepeat: 'no-repeat', | ||
width: '17px', | ||
height: '17px', | ||
display: 'inline-block', | ||
}} | ||
></div> | ||
{showPublicCode && ( | ||
<span style={{ verticalAlign: 'top', fontSize: '14px', paddingLeft: '2px' }}>{leg.line?.publicCode}</span> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* textColor can be black or white. White for dark colors and black for light colors. | ||
* Calculated based on luminance formula: | ||
* sqrt( 0.299*Red^2 + 0.587*Green^2 + 0.114*Blue^2 ) | ||
*/ | ||
export function generateTextColor(hexColor: string) { | ||
const color = decodeColor(hexColor); | ||
|
||
//Calculates luminance based on https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color | ||
const newRed = 0.299 * Math.pow(color[0] / 255.0, 2.0); | ||
const newGreen = 0.587 * Math.pow(color[1] / 255.0, 2.0); | ||
const newBlue = 0.114 * Math.pow(color[2] / 255.0, 2.0); | ||
const luminance = Math.sqrt(newRed + newGreen + newBlue); | ||
|
||
if (luminance > 0.66) { | ||
return '#000'; | ||
} else { | ||
return '#fff'; | ||
} | ||
} | ||
|
||
function decodeColor(hex: string): number[] { | ||
return hex2rgb(hex); | ||
} | ||
|
||
function hex2rgb(hex: string) { | ||
if (hex.length === 4) { | ||
return fullHex(hex); | ||
} | ||
|
||
return [parseInt(hex.slice(1, 3), 16), parseInt(hex.slice(3, 5), 16), parseInt(hex.slice(5, 7), 16)]; | ||
} | ||
|
||
function fullHex(hex: string) { | ||
let r = hex.slice(1, 2); | ||
let g = hex.slice(2, 3); | ||
let b = hex.slice(3, 4); | ||
|
||
return [parseInt(r + r, 16), parseInt(g + g, 16), parseInt(b + b, 16)]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Mode } from '../gql/graphql.ts'; | ||
|
||
export function isTransitMode(mode: Mode) { | ||
return ( | ||
mode === Mode.Rail || | ||
mode === Mode.Coach || | ||
mode === Mode.Metro || | ||
mode === Mode.Bus || | ||
mode === Mode.Tram || | ||
mode === Mode.Water || | ||
mode === Mode.Air || | ||
mode === Mode.Cableway || | ||
mode === Mode.Funicular || | ||
mode === Mode.Trolleybus || | ||
mode === Mode.Monorail || | ||
mode === Mode.Taxi | ||
); | ||
} |