Skip to content

Instantly share code, notes, and snippets.

@adi518
Last active June 30, 2018 21:22
Show Gist options
  • Save adi518/c78ff96528eb16e9297a05182265f30a to your computer and use it in GitHub Desktop.
Save adi518/c78ff96528eb16e9297a05182265f30a to your computer and use it in GitHub Desktop.
import isNumber from 'lodash.isnumber'
export function toDevicePixelRatio(size, options = {}) {
const {
ratio = 2,
scale = 1,
even = true
} = options
const evenFactor = even ? 2 : 1
const floatSize = parseFloat(size)
const interpolated = evenFactor * Math.round((floatSize * scale / ratio) / evenFactor)
if (isNumber(size)) {
return interpolated
}
const unit = size.substring(2)
return `${interpolated}${unit}`
}
// Alias
export const toDpr = toDevicePixelRatio
@function extend-map($source, $target) {
@return map-merge($source, $target);
}
@function to-dpr($options: ()) {
$options: extend-map((
'size': null,
'even': true,
'ratio': 2,
'scale': 1,
), $options);
$size: map-get($options, 'size');
$even: map-get($options, 'even');
$scale: map-get($options, 'scale');
$ratio: map-get($options, 'ratio');
$even-factor: 1;
@if ($even) {
$even-factor: 2;
}
@return $even-factor * round($size * $scale / $ratio / $even-factor);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment