|
| 1 | +function gcd(a: number, b: number): number { |
| 2 | + if (b === 0) { |
| 3 | + return a; |
| 4 | + } |
| 5 | + return gcd(b, a % b); |
| 6 | +} |
| 7 | + |
| 8 | +export interface AspectRatio { |
| 9 | + width: number; |
| 10 | + height: number; |
| 11 | +} |
| 12 | + |
| 13 | +const HORIZONTAL_ASPECT_RATIO = [ |
| 14 | + { width: 4, height: 4 }, // Square |
| 15 | + { width: 4, height: 3 }, // Standard Fullscreen |
| 16 | + { width: 16, height: 10 }, // Standard LCD |
| 17 | + { width: 16, height: 9 }, // HD |
| 18 | + // { width: 37, height: 20 }, // Widescreen |
| 19 | + { width: 6, height: 3 }, // Univisium |
| 20 | + { width: 21, height: 9 }, // Anamorphic 2.35:1 |
| 21 | + // { width: 64, height: 27 }, // Anamorphic 2.39:1 or 2.37:1 |
| 22 | + { width: 19, height: 16 }, // Movietone |
| 23 | + { width: 5, height: 4 }, // 17' LCD CRT |
| 24 | + // { width: 48, height: 35 }, // 16mm and 35mm |
| 25 | + { width: 11, height: 8 }, // 35mm full sound |
| 26 | + // { width: 143, height: 100 }, // IMAX |
| 27 | + { width: 6, height: 4 }, // 35mm photo |
| 28 | + { width: 14, height: 9 }, // commercials |
| 29 | + { width: 5, height: 3 }, // Paramount |
| 30 | + { width: 7, height: 4 }, // early 35mm |
| 31 | + { width: 11, height: 5 }, // 70mm |
| 32 | + { width: 12, height: 5 }, // Bluray |
| 33 | + { width: 8, height: 3 }, // Super 16 |
| 34 | + { width: 18, height: 5 }, // IMAX |
| 35 | + { width: 12, height: 3 }, // Polyvision |
| 36 | +]; |
| 37 | + |
| 38 | +const VERTICAL_ASPECT_RATIO = HORIZONTAL_ASPECT_RATIO.map(item => ({ |
| 39 | + width: item.height, |
| 40 | + height: item.width, |
| 41 | +})); |
| 42 | + |
| 43 | +const ASPECT_RATIO = [...HORIZONTAL_ASPECT_RATIO, ...VERTICAL_ASPECT_RATIO]; |
| 44 | + |
| 45 | +export function getAspectRatio({ width, height }: AspectRatio): AspectRatio { |
| 46 | + const denom = gcd(width, height); |
| 47 | + |
| 48 | + return { |
| 49 | + width: width / denom, |
| 50 | + height: height / denom, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +export function getNearestAspectRatio(ratio: AspectRatio): AspectRatio { |
| 55 | + let nearest = Number.MAX_VALUE; |
| 56 | + let id = 0; |
| 57 | + |
| 58 | + const originalRatio = ratio.width / ratio.height; |
| 59 | + |
| 60 | + for (let i = 0; i < ASPECT_RATIO.length; i += 1) { |
| 61 | + const target = ASPECT_RATIO[i]!; |
| 62 | + |
| 63 | + const tRatio = target.width / target.height; |
| 64 | + const squared = tRatio - originalRatio; |
| 65 | + const distance = Math.sqrt(squared * squared); |
| 66 | + |
| 67 | + if (i === 0) { |
| 68 | + nearest = distance; |
| 69 | + } else if (distance < nearest) { |
| 70 | + id = i; |
| 71 | + nearest = distance; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return ASPECT_RATIO[id]!; |
| 76 | +} |
| 77 | + |
| 78 | +export function getScaledComponentRatio(ratio: AspectRatio): AspectRatio { |
| 79 | + const xScale = 9 / ratio.width; |
| 80 | + const yScale = 9 / ratio.height; |
| 81 | + const scale = Math.min(xScale, yScale); |
| 82 | + return { |
| 83 | + width: scale * ratio.width, |
| 84 | + height: scale * ratio.height, |
| 85 | + }; |
| 86 | +} |
0 commit comments