Skip to content

Commit

Permalink
fix: parse hex color and extract opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Mar 13, 2023
1 parent c571777 commit b8c78b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/component/1d/Line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useScaleChecked } from '../context/ScaleContext';
import useActiveSpectrumStyleOptions from '../hooks/useActiveSpectrumStyleOptions';
import useXYReduce, { XYReducerDomainAxis } from '../hooks/useXYReduce';
import { PathBuilder } from '../utility/PathBuilder';
import { parseColor } from '../utility/parseColor';

interface LineProps {
data?: {
Expand Down Expand Up @@ -41,16 +42,18 @@ function Line({ data, id, display, index }: LineProps) {
}
}, [scaleX, scaleY, id, data, xyReduce]);

const { color: stroke, opacity: strokeOpacity } = parseColor(
display?.color || 'black',
);

return (
<path
className="line"
data-test-id="spectrum-line"
key={id}
stroke={display.color}
stroke={stroke}
fill="none"
style={{
opacity,
}}
strokeOpacity={opacity === 1 ? strokeOpacity : opacity}
d={paths}
transform={`translate(0,-${shiftY * index})`}
/>
Expand Down
20 changes: 20 additions & 0 deletions src/component/utility/parseColor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function parseColor(color: string) {
if (!color.startsWith('#')) {
return { color, opacity: 1 };
}

const hex = color.replace('#', '');

if (hex.length === 3) {
return {
color: `#${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}`,
opacity: 1,
};
}

const hexOpacity = hex.slice(6) || 'FF';
const opacity =
Math.round((Number.parseInt(hexOpacity, 16) / 255) * 100) / 100;

return { color: `#${hex.slice(0, 6)}`, opacity };
}

0 comments on commit b8c78b6

Please sign in to comment.