Skip to content

Instantly share code, notes, and snippets.

@jakob-e
Last active August 29, 2015 14:13
Show Gist options
  • Save jakob-e/544566f3e147141f572c to your computer and use it in GitHub Desktop.
Save jakob-e/544566f3e147141f572c to your computer and use it in GitHub Desktop.
Generated by SassMeister.com. http://sassmeister.com/gist/544566f3e147141f572c
//
// SCSS Unit Conversion v.2.0
//
// ----
// Sass (v3.4.9)
// Compass (v1.0.1)
// ----
// ___________________________________________________________________
//
// Unit Conversion
// ___________________________________________________________________
$base-font-size: 16px !default;
// Absolute lengths
@function px($input){ @return convert(px, $input); }
@function pt($input){ @return convert(pt, $input); }
@function pc($input){ @return convert(pc, $input); }
@function in($input){ @return convert(in, $input); }
@function mm($input){ @return convert(mm, $input); }
@function cm($input){ @return convert(cm, $input); }
// Angles
@function deg($input){ @return convert(deg, $input); }
@function rad($input){ @return convert(rad, $input); }
@function grad($input){ @return convert(grad, $input); }
@function turn($input){ @return convert(turn, $input); }
// Resolution
@function dpi($input){ @return convert(dpi, $input); }
@function dpcm($input){ @return convert(dpcm, $input); }
@function dppx($input){ @return convert(dppx, $input); }
// Time
@function ms($input){ @return convert(ms, $input); }
@function s($input){ @return convert(s, $input); }
// Frequencies
@function hz($input){ @return convert(hz, $input);}
@function khz($input){ @return convert(khz, $input); }
// Relative lengths
@function em($input...){
$em: convert(em, nth($input,1));
// Adjust for compounds (visual size)
@if length($input) > 1 {
@for $i from 2 through length($input){ $em: $em / num(em(nth($input,$i))); }
}
@return $em;
}
@function rem($input){ @return convert(rem, num(em($input))); }
// Inconvertible relative lengths – depends on font
@function ex($input){ @return convert(ex, $input); }
@function ch($input){ @return convert(ch, $input); }
// Viewport
@function vw($input){ @return convert(vw, $input); }
@function vh($input){ @return convert(vh, $input); }
@function vmin($input){ @return convert(vmin, $input); }
@function vmax($input){ @return convert(vmax, $input); }
// Strings and numbers
@function str($input){ @return #{$input}; }
@function num($input){
@if type-of($input) != number {
@error 'Could not convert `#{$input}` - must be `type-of number`';
@return null;
}
@return $input/($input*0+1);
}
@function int($input){
$num: num($input);
@return if($num<0, ceil($num), floor($num));
}
@function uint($input){ @return abs(int($input)); }
// Conversion function
@function convert($unit, $input){
// Maybe add convert from string - dodgy fella ;)
// Test against valid CSS units
$convert-unit: map-get((
px: 0px, pt: 0pt, pc: 0pc, in: 0in, mm: 0mm, cm: 0cm, // absolute length
em: 0em, rem: 0rem, ch: 0ch, ex: 0ex, // relative length - font based
vw: 0vw, vh: 0vh, vmin: 0vmin, vmax: 0vmax, // relative length - viewport based
deg: 0deg, turn: 0turn, grad: 0grad, rad: 0rad, // angle
s: 0s, ms: 0ms, // time
hz: 0Hz, khz: 0kHz, // frequencie
dpi: 0dpi, dpcm: 0dpcm, dppx: 0dppx, // resolution
pct: 0%, percent: 0%, num: 0, number: 0 // percent or number
), $unit);
// Error handling – wrong $unit
// Incomparable units are caught in convertion
@if not $convert-unit {
@error 'Could not convert to `#{$unit}` – must be a valid CSS unit';
@return null;
}
// Number/incomparable conversion
@if index(num number ex ch vw vh vmin vmax, $unit) {
$value: num($input);
}
// EM/REM convertion using px as base
@if index(em rem, unit($input)) {
$input: 0px + num($input) * $base-font-size/1px;
}
@if index(em rem, $unit) and not unitless($input) {
$input: 0px + $input;
$input: num($input) * 1px/$base-font-size;
}
// Convert
@return $convert-unit + $input;
}
// Aliases
@function string($input){ @return str($input);}
@function number($input){ @return num($input);}
@function parse-float($input){ @return num($input);}
@function parse-int($input){ @return int($input);}
//
// SCSS Unit Conversion v.2.0
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment