|
import { createMemoizedSelector } from 'app/store/createMemoizedSelector'; |
|
import type { Dimensions } from 'features/controlLayers/store/types'; |
|
import { selectGallerySlice } from 'features/gallery/store/gallerySlice'; |
|
import type { ComparisonFit } from 'features/gallery/store/types'; |
|
import type { ImageDTO } from 'services/api/types'; |
|
|
|
export const DROP_SHADOW = 'drop-shadow(0px 0px 4px rgb(0, 0, 0)) drop-shadow(0px 0px 4px rgba(0, 0, 0, 0.3))'; |
|
|
|
export type ComparisonProps = { |
|
firstImage: ImageDTO; |
|
secondImage: ImageDTO; |
|
containerDims: Dimensions; |
|
}; |
|
|
|
export const fitDimsToContainer = (containerDims: Dimensions, imageDims: Dimensions): Dimensions => { |
|
|
|
if (containerDims.width === 0 || containerDims.height === 0) { |
|
return { width: imageDims.width, height: imageDims.height }; |
|
} |
|
|
|
|
|
if (imageDims.width <= containerDims.width && imageDims.height <= containerDims.height) { |
|
return { width: imageDims.width, height: imageDims.height }; |
|
} |
|
|
|
const targetAspectRatio = containerDims.width / containerDims.height; |
|
const imageAspectRatio = imageDims.width / imageDims.height; |
|
|
|
let width: number; |
|
let height: number; |
|
|
|
if (imageAspectRatio > targetAspectRatio) { |
|
|
|
width = containerDims.width; |
|
height = width / imageAspectRatio; |
|
} else { |
|
|
|
height = containerDims.height; |
|
width = height * imageAspectRatio; |
|
} |
|
return { width, height }; |
|
}; |
|
|
|
|
|
|
|
|
|
export const getSecondImageDims = ( |
|
comparisonFit: ComparisonFit, |
|
fittedDims: Dimensions, |
|
firstImageDims: Dimensions, |
|
secondImageDims: Dimensions |
|
): Dimensions => { |
|
const width = |
|
comparisonFit === 'fill' ? fittedDims.width : (fittedDims.width * secondImageDims.width) / firstImageDims.width; |
|
const height = |
|
comparisonFit === 'fill' ? fittedDims.height : (fittedDims.height * secondImageDims.height) / firstImageDims.height; |
|
|
|
return { width, height }; |
|
}; |
|
export const selectComparisonImages = createMemoizedSelector(selectGallerySlice, (gallerySlice) => { |
|
const firstImage = gallerySlice.selection.slice(-1)[0] ?? null; |
|
const secondImage = gallerySlice.imageToCompare; |
|
return { firstImage, secondImage }; |
|
}); |
|
|