Browse Source

Introduce custom resolutions

customisations
alemart 11 months ago
parent
commit
39a437a2a9
2 changed files with 46 additions and 20 deletions
  1. 15
    14
      docs/api/resolution.md
  2. 31
    6
      src/utils/resolution.ts

+ 15
- 14
docs/api/resolution.md View File

@@ -4,17 +4,18 @@ A `Resolution` is a setting defined by a string. It is mapped to a size measured
4 4
 
5 5
 The table below shows examples of how resolution strings are converted to pixels:
6 6
 
7
-| Resolution | 16:9 | 16:10 | 4:3 | Notes |
8
-| ---------- | ---- | ----- | --- | ----- |
9
-| `"xs"` | 214x120 | 192x120 | 160x120 | |
10
-| `"xs+"` | 256x144 | 230x144 | 192x144 | 144p |
11
-| `"sm"` | 426x240 | 384x240 | 320x240 | 240p |
12
-| `"sm+"` | 512x288 | 460x288 | 384x288 | |
13
-| `"md"` | 568x320 | 512x320 | 426x320 | |
14
-| `"md+"` | 640x360 | 576x360 | 480x360 | 360p |
15
-| `"lg"` | 854x480 | 768x480 | 640x480 | 480p |
16
-| `"lg+"` | 1066x600 | 960x600 | 800x600 | |
17
-| `"xl"` | 1280x720 | 1152x720 | 960x720 | 720p |
18
-| `"xl+"` | 1366x768 | 1228x768 | 1024x768 | |
19
-| `"xxl"` | 1600x900 | 1440x900 | 1200x900 | |
20
-| `"xxl+"` | 1706x960 | 1536x960 | 1280x960 | |
7
+| Resolution | Alias | 16:9 | 16:10 | 4:3 | Notes |
8
+| ---------- | ----- | ---- | ----- | --- | ----- |
9
+| `"120p"` | `"xs"` | 214x120 | 192x120 | 160x120 | |
10
+| `"144p"` | `"xs+"` | 256x144 | 230x144 | 192x144 | |
11
+| `"240p"` | `"sm"` | 426x240 | 384x240 | 320x240 | SD |
12
+| `"288p"` | `"sm+"` | 512x288 | 460x288 | 384x288 | |
13
+| `"320p"` | `"md"` | 568x320 | 512x320 | 426x320 | |
14
+| `"360p"` | `"md+"` | 640x360 | 576x360 | 480x360 | SD |
15
+| `"480p"` | `"lg"` | 854x480 | 768x480 | 640x480 | SD |
16
+| `"600p"` | `"lg+"` | 1066x600 | 960x600 | 800x600 | |
17
+| `"720p"` | `"xl"` | 1280x720 | 1152x720 | 960x720 | HD |
18
+| `"768p"` | `"xl+"` | 1366x768 | 1228x768 | 1024x768 | |
19
+| `"900p"` | `"xxl"` | 1600x900 | 1440x900 | 1200x900 | |
20
+| `"960p"` | `"xxl+"` | 1706x960 | 1536x960 | 1280x960 | |
21
+| `"1080p"` | | 1920x1080 | 1728x1080 | 1440x1080 | Full HD |

+ 31
- 6
src/utils/resolution.ts View File

@@ -24,11 +24,21 @@ import Speedy from 'speedy-vision';
24 24
 import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
25 25
 import { IllegalArgumentError } from './errors';
26 26
 
27
+type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
28
+type EvenDigit = '0' | '2' | '4' | '6' | '8';
29
+type PositiveDigit = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
30
+type OptionalDigit = Digit | '';
31
+type CustomResolution = `${PositiveDigit}${OptionalDigit}${Digit}${EvenDigit}p`;
32
+type ResolutionAlias = 'xs' | 'xs+' | 'sm' | 'sm+' | 'md' | 'md+' | 'lg' | 'lg+' | 'xl' | 'xl+' | 'xxl' | 'xxl+';
33
+
27 34
 /** Resolution type */
28
-export type Resolution = 'xs' | 'xs+' | 'sm' | 'sm+' | 'md' | 'md+' | 'lg' | 'lg+' | 'xl' | 'xl+' | 'xxl' | 'xxl+';
35
+export type Resolution = ResolutionAlias | CustomResolution;
36
+
37
+/** A regex that identifies custom resolutions */
38
+const CUSTOM_RESOLUTION_REGEX = /^[1-9][0-9]?[0-9][02468]p$/;
29 39
 
30
-/** Reference heights when in landscape mode, measured in pixels */
31
-const REFERENCE_HEIGHT: { readonly [R in Resolution]: number } = {
40
+/** Reference heights when in landscape mode, measured in pixels, for all aliases */
41
+const ALIAS_TO_HEIGHT: { readonly [R in ResolutionAlias]: number } = {
32 42
     'xs' : 120,
33 43
     'xs+': 144,
34 44
     'sm' : 240,
@@ -44,7 +54,6 @@ const REFERENCE_HEIGHT: { readonly [R in Resolution]: number } = {
44 54
     //'ul-': 1024,
45 55
     //'ul': 1080, // what should we call this? xxxl? ul? (ultra large?)
46 56
     //'ul+': 1200,
47
-    // instead of defining xxxl, what if we accept custom resolution names such as "720p" and "1080p"?
48 57
 };
49 58
 
50 59
 /**
@@ -55,10 +64,10 @@ const REFERENCE_HEIGHT: { readonly [R in Resolution]: number } = {
55 64
  */
56 65
 export function computeResolution(resolution: Resolution, aspectRatio: number): SpeedySize
57 66
 {
58
-    const referenceHeight = REFERENCE_HEIGHT[resolution];
67
+    const referenceHeight = parseHeight(resolution);
59 68
     let width = 0, height = 0;
60 69
 
61
-    if(referenceHeight === undefined)
70
+    if(Number.isNaN(referenceHeight))
62 71
         throw new IllegalArgumentError('Invalid resolution: ' + resolution);
63 72
     else if(aspectRatio <= 0)
64 73
         throw new IllegalArgumentError('Invalid aspect ratio: ' + aspectRatio);
@@ -77,4 +86,20 @@ export function computeResolution(resolution: Resolution, aspectRatio: number):
77 86
     }
78 87
 
79 88
     return Speedy.Size(width, height);
89
+}
90
+
91
+/**
92
+ * Get the height in pixels of a resolution
93
+ * @param resolution resolution type
94
+ * @returns the height in pixels, or NaN on error
95
+ */
96
+function parseHeight(resolution: Resolution): number
97
+{
98
+    if(ALIAS_TO_HEIGHT.hasOwnProperty(resolution))
99
+        return ALIAS_TO_HEIGHT[resolution as ResolutionAlias];
100
+
101
+    if(CUSTOM_RESOLUTION_REGEX.test(resolution))
102
+        return parseInt(resolution);
103
+
104
+    return Number.NaN;
80 105
 }

Loading…
Cancel
Save