Aspect Ratio Calculator
Calculate simplified aspect ratios from pixel dimensions, find missing dimensions from a known ratio, and report decimal and percentage equivalents using GCD simplification.
Input
- - Two dimensions (width and height in pixels), OR
- A known ratio (e.g.,
16:9) and one dimension (width or height), to calculate the missing value
Output
- - Simplified ratio (e.g.,
16:9) - Decimal ratio (e.g.,
1.7778) - Percentage (width as % of height, e.g.,
177.78%) - If solving for missing dimension: the calculated value
Instructions
- 1. Parse the user's input to determine the mode:
-
Mode A: two pixel dimensions given → calculate ratio and metadata
-
Mode B: ratio + one dimension given → calculate the missing dimension
- 2. Mode A — Ratio from dimensions:
a. Compute
divisor = GCD(width, height) using the Euclidean algorithm:
GCD(a, b) = b === 0 ? a : GCD(b, a % b)
b. Simplified ratio:
(width / divisor):(height / divisor)
c. Decimal:
(width / height).toFixed(4)
d. Percentage:
(width / height * 100).toFixed(2)%
- 3. Mode B — Dimension from ratio:
a. Parse the ratio as
rW:rH
b. If width is given:
height = round(width * rH / rW)
c. If height is given:
width = round(height * rW / rH)
- 4. Identify if the ratio matches a known standard and name it:
-
16:9 → HD Video / Widescreen
-
4:3 → Classic TV / Standard
-
1:1 → Square
-
21:9 → Ultrawide
-
9:16 → Mobile Video / Story
-
3:4 → Portrait Classic
-
3:2 → 35mm Film
-
2:3 → Portrait Photo
- 5. Output all results clearly.
Options
- -
width: positive integer - INLINECODE21 : positive integer
- INLINECODE22 : string in
W:H format — for Mode B - INLINECODE24 : if true, preserve the current ratio when one dimension changes
Examples
Input: "What is the aspect ratio of 1920x1080?"
Output:
- - Simplified ratio: 16:9 (HD Video / Widescreen)
- Decimal: 1.7778
- Percentage: 177.78% (width is 177.78% of height)
Input: "I have a 16:9 video, width is 1280px — what height do I need?"
Output:
- - Width: 1280px, Ratio: 16:9
- Height: 720px
- (1280 × 9 / 16 = 720)
Input: "Simplify 2560x1440"
Output:
- - Simplified ratio: 16:9 (HD Video / Widescreen)
- Decimal: 1.7778
- Percentage: 177.78%
Input: "What is the aspect ratio of 800x600?"
Output:
- - Simplified ratio: 4:3 (Classic TV / Standard)
- Decimal: 1.3333
- Percentage: 133.33%
Input: "Give me height for a 21:9 ultrawide at 2560px width"
Output:
- - Width: 2560px, Ratio: 21:9
- Height: 1097px
- (2560 × 9 / 21 ≈ 1097)
Error Handling
- - If either dimension is 0 or negative, return an error: "Dimensions must be positive integers greater than 0."
- If a ratio is provided in an invalid format (not
W:H), ask the user to use the W:H format (e.g., 16:9). - If both dimensions result in the same ratio numerator and denominator equal to 1 (e.g., prime dimensions with no common factor), output the ratio as-is and note it cannot be simplified further.
宽高比计算器
根据像素尺寸计算简化后的宽高比,通过已知比例求缺失尺寸,并使用最大公约数简化法输出小数和百分比等效值。
输入
- - 两个尺寸(以像素为单位的宽度和高度),或
- 已知比例(例如 16:9)和一个尺寸(宽度或高度),用于计算缺失值
输出
- - 简化后的比例(例如 16:9)
- 小数比例(例如 1.7778)
- 百分比(宽度占高度的百分比,例如 177.78%)
- 若求解缺失尺寸:计算得出的值
操作说明
- 1. 解析用户输入以确定模式:
-
模式A:给定两个像素尺寸 → 计算比例和元数据
-
模式B:给定比例 + 一个尺寸 → 计算缺失尺寸
- 2. 模式A — 从尺寸计算比例:
a. 使用欧几里得算法计算 除数 = GCD(宽度, 高度):GCD(a, b) = b === 0 ? a : GCD(b, a % b)
b. 简化比例:(宽度 / 除数):(高度 / 除数)
c. 小数:(宽度 / 高度).toFixed(4)
d. 百分比:(宽度 / 高度 * 100).toFixed(2)%
- 3. 模式B — 从比例计算尺寸:
a. 将比例解析为 rW:rH
b. 若给定宽度:高度 = round(宽度 * rH / rW)
c. 若给定高度:宽度 = round(高度 * rW / rH)
- 4. 判断比例是否匹配已知标准并命名:
- 16:9 → 高清视频/宽屏
- 4:3 → 经典电视/标准
- 1:1 → 正方形
- 21:9 → 超宽屏
- 9:16 → 手机视频/故事模式
- 3:4 → 经典竖版
- 3:2 → 35mm胶片
- 2:3 → 竖版照片
- 5. 清晰输出所有结果。
选项
- - width:正整数
- height:正整数
- ratio:W:H格式的字符串 — 用于模式B
- locked:若为true,当一个尺寸变化时保持当前比例
示例
输入:1920x1080的宽高比是多少?
输出:
- - 简化比例:16:9(高清视频/宽屏)
- 小数:1.7778
- 百分比:177.78%(宽度是高度的177.78%)
输入:我有一个16:9的视频,宽度为1280px — 我需要多高?
输出:
- - 宽度:1280px,比例:16:9
- 高度:720px
- (1280 × 9 / 16 = 720)
输入:简化2560x1440
输出:
- - 简化比例:16:9(高清视频/宽屏)
- 小数:1.7778
- 百分比:177.78%
输入:800x600的宽高比是多少?
输出:
- - 简化比例:4:3(经典电视/标准)
- 小数:1.3333
- 百分比:133.33%
输入:给我21:9超宽屏在2560px宽度下的高度
输出:
- - 宽度:2560px,比例:21:9
- 高度:1097px
- (2560 × 9 / 21 ≈ 1097)
错误处理
- - 若任一尺寸为0或负数,返回错误:尺寸必须为大于0的正整数。
- 若提供的比例格式无效(非W:H格式),提示用户使用W:H格式(例如16:9)。
- 若两个尺寸计算出的比例分子和分母都等于1(例如无公因数的质数尺寸),按原样输出比例并注明无法进一步简化。