Skip to content

Instantly share code, notes, and snippets.

@marlun78
Created May 18, 2015 08:00
Show Gist options
  • Save marlun78/54bbf62e249f36c3aed1 to your computer and use it in GitHub Desktop.
Save marlun78/54bbf62e249f36c3aed1 to your computer and use it in GitHub Desktop.
Takes a screen dimension and return its ratio as width:height
/**
* toRatio.js
* Copyright (c) 2015 marlun78
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83
*
* Takes a screen dimension and return its ratio as width:height.
*
* @example
* toRatio(1280, 720); //=> 16:9
* toRatio(800, 600); //=> 4:3
*
* @param {number} width
* @param {number} height
* @returns {string}
*/
function toRatio(width, height) {
if (width % 10 === 0 && height % 10 === 0) {
return toRatio(width / 10, height / 10);
} else if (width % 2 === 0 && height % 2 === 0) {
return toRatio(width / 2, height / 2);
} else {
return width + ':' + height;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment