Last active
June 30, 2018 21:22
-
-
Save adi518/c78ff96528eb16e9297a05182265f30a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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