show how the current ratio differs from the closest popular format

This commit is contained in:
Piotr Beling 2023-05-17 09:22:02 +02:00
parent ef48fc2425
commit b47a1393e8
2 changed files with 17 additions and 3 deletions

View file

@ -518,7 +518,7 @@
<property name="xpad">8</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">5</property>
<property name="width_chars">17</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</object>

View file

@ -40,10 +40,24 @@ def _(s): return s # TODO: i18n
DRAG_BL, DRAG_B, DRAG_BR
) = list(range(10))
POPULAR_FORMATS = ((1, 1), (4, 3), (3, 2), (16, 9))
def closest_ratio(ratio, formats=POPULAR_FORMATS):
n, d = min(formats, key=lambda f: abs(f[0]/f[1]-ratio)) # find the format closest to the ratio
s = ratio*d-n
return f'{n}{s:+.2f}', d
def describe_ratio(a, b):
if a == 0 or b == 0: return "degenerate"
if a > b: return "%.2f:1" % (a*1./b)
return "1:%.2f" % (b*1./a)
if a > b:
ratio = a*1./b
f,d = closest_ratio(ratio)
return f'{ratio:.2f}:1 | {f}:{d}'
else:
ratio = b*1./a
f,d = closest_ratio(ratio)
return f'1:{ratio:.2f} | {d}:{f}'
def clamp(value, low, high):
if value < low: return low