├── package.json ├── README.md └── _unitconversion.scss /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "unitconversion", 3 | "version": "2.2.0", 4 | "description": "Simple SCSS functions for unitconversion", 5 | "scripts": { 6 | "test": "echo \"Error: no test specified\" && exit 1" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "git+https://github.com/jakob-e/unitconversion.git" 11 | }, 12 | "keywords": [ 13 | "SCSS", 14 | "Unit", 15 | "Conversion", 16 | "Unitconversion" 17 | ], 18 | "author": "Jakob E ", 19 | "license": "ISC", 20 | "bugs": { 21 | "url": "https://github.com/jakob-e/unitconversion/issues" 22 | }, 23 | "homepage": "https://github.com/jakob-e/unitconversion#readme" 24 | } 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | _unitconversion.scss (v.2.2.0) 2 | ============== 3 | 4 | **Convert Anything** (almost) 5 | 6 | At the moment the following units are supported (please comment if something is wrong/missing): 7 | 8 | ````SCSS 9 | px, pt, pc, in, mm, cm, em, rem, ex, ch, vw, vh, vmin, vmax, 10 | deg, rad, grad, turn, dpi, dpcm, dppx, s, ms, hz, khz, number, 11 | ratio 12 | ```` 13 | 14 | **NPM** 15 | ```` 16 | npm install unitconversion 17 | ```` 18 | 19 | 20 | **Simple Syntax** 21 | 22 | Rather than going with the "good" old fromUnit-to-toUnit(fromUnit) syntax this function set is based on toUnit(fromUnit) making it a lot shorter and less cumbersome to maintain – as shown below :-) 23 | 24 | ````SCSS 25 | pt-to-px(input); 26 | pc-to-px(input); 27 | in-to-px(input); 28 | mm-to-px(input); ==> px(input); 29 | cm-to-px(input); 30 | em-to-px(input); 31 | rem-to-px(input); 32 | num-to-px(input); 33 | ````` 34 | **Note!** 35 | 36 | * To allow conversion between relative and absolute lengths em and rem are calculated as pixel values based on `$root-font-size` and `$base-font-size` (16px browser default). If you change the base font size of your document – remember to set the `$root-font-size` and `$base-font-size` variables accordingly. 37 | 38 | * To ease the job of handling compounds the em function takes multiple arguments. Each argument is treated as an em compound and used to calculate the visual size. 39 | Example – how to set a visual size of 18px on a class nested in an element with a font-size of 2em: 40 | ````SCSS 41 | .class { 42 | font-size: em(18px, 2em); ==> 0.5625em; 43 | } 44 | ````` 45 | * If you use unit conversion in relation to the font shorthand syntax be aware that line-height "/" will cause division. To prevent this from happening you can either either interpolate the value or use + to add the pieces together without calculation: 46 | ````SCSS 47 | h2 { 48 | font:300 #{em(24px)}/3 'Lato', sans-serif; 49 | } 50 | ```` 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 |
Conversion table
TypeFunctionInput units
Absolute lengthpx($input);px, pt, pc, in, mm, cm, em, rem, number
pt($input);px, pt, pc, in, mm, cm, em, rem, number
pc($input);px, pt, pc, in, mm, cm, em, rem, number
in($input);px, pt, pc, in, mm, cm, em, rem, number
mm($input);px, pt, pc, in, mm, cm, em, rem, number
cm($input);px, pt, pc, in, mm, cm, em, rem, number
Relative lengthem($input);px, pt, pc, in, mm, cm, em, rem, number
rem($input);px, pt, pc, in, mm, cm, em, rem, number
ex($input);ex, number
ch($input);ch, number
vw($input);vw, number
vh($input);vh, number
vmin($input);vmin, number
vmax($input);vmax, number
Angle deg($input);deg, rad, grad, turn, number
rad($input);deg, rad, grad, turn, number
grad($input);deg, rad, grad, turn, number
turn($input);deg, rad, grad, turn, number
Resolution dpi($input);dpi, dpcm, dppx, number
dpcm($input);dpi, dpcm, dppx, number
dppx($input);dpi, dpcm, dppx, number
Time ms($input);ms, s, number
s($input); ms, s, number
Frequency hz($input);Hz, kHz, number
khz($input);Hz, kHz, number
String str($input);
string($input);
Anything not null
Number num(input);
number($input)
px, pt, pc, in, mm, cm, em, rem, ex, ch,
vw, vh, vmin, vmax, deg, rad, grad, turn,
dpi, dpcm, dppx, s, ms, hz, khz, number
int($input);px, pt, pc, in, mm, cm, em, rem, ex, ch,
vw, vh, vmin, vmax, deg, rad, grad, turn,
dpi, dpcm, dppx, s, ms, hz, khz, number
uint($input);px, pt, pc, in, mm, cm, em, rem, ex, ch,
vw, vh, vmin, vmax, deg, rad, grad, turn,
dpi, dpcm, dppx, s, ms, hz, khz, number
94 | 95 | 96 | Cive it a try on: 97 | [Sassmeister.com](http://www.sassmeister.com/gist/5943041498e406ff2d1d452ac2c31c9f) 98 | 99 | 100 |
101 | 102 | 103 | [The MIT License (MIT)](https://opensource.org/licenses/MIT) 104 | Copyright (c) 2016 Jakob Eriksen 105 | 106 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 107 | 108 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 109 | 110 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 111 | -------------------------------------------------------------------------------- /_unitconversion.scss: -------------------------------------------------------------------------------- 1 | // ____________________________________________________________________________ 2 | // 3 | // Unit Conversion v.2.2.0 4 | // npm install unitconversion 5 | // https://github.com/jakob-e/unitconversion 6 | // ____________________________________________________________________________ 7 | // 8 | // Function Input units 9 | // 10 | // Absolute length 11 | // px(input); px, pt, pc, in, mm, cm, em, rem, number 12 | // pt(input); px, pt, pc, in, mm, cm, em, rem, number 13 | // pc(input); px, pt, pc, in, mm, cm, em, rem, number 14 | // in(input); px, pt, pc, in, mm, cm, em, rem, number 15 | // mm(input); px, pt, pc, in, mm, cm, em, rem, number 16 | // cm(input); px, pt, pc, in, mm, cm, em, rem, number 17 | // 18 | // Relative length 19 | // em(input); px, pt, pc, in, mm, cm, em, rem, number 20 | // rem(input); px, pt, pc, in, mm, cm, em, rem, number 21 | // ex(input); ex, number 22 | // ch(input); ch, number 23 | // vw(input); vw, number 24 | // vh(input); vh, number 25 | // vmin(input); vmin, number 26 | // vmax(input); vmax, number 27 | // 28 | // Angle 29 | // deg(input); deg, rad, grad, turn, number 30 | // rad(input); deg, rad, grad, turn, number 31 | // grad(input); deg, rad, grad, turn, number 32 | // turn(input); deg, rad, grad, turn, number 33 | // 34 | // Resolution 35 | // dpi(input); dpi, dpcm, dppx, number 36 | // dpcm(input); dpi, dpcm, dppx, number 37 | // dppx(input); dpi, dpcm, dppx, number 38 | // 39 | // Time 40 | // s(input); s, ms, number 41 | // ms(input); s, ms, number 42 | // 43 | // Frequency 44 | // hz(input); hz, khz, number 45 | // khz(input); hz, khz, number 46 | // 47 | // String 48 | // str(input); anything not null 49 | // 50 | // Number, int and uint 51 | // num(input); px, pt, pc, in, mm, cm, em, rem, ex, ch, 52 | // vw, vh, vmin, vmax, deg, rad, grad, turn, 53 | // dpi, dpcm, dppx, s, ms, hz, khz, number 54 | // int(input); as number 55 | // uint(input); as number 56 | // 57 | // ratio number to fraction 58 | // 59 | // Aliases 60 | // string(input); 61 | // number(input); 62 | // 63 | // ____________________________________________________________________________ 64 | 65 | // Base font size in pixel for converting em and rem to absolute lengths. 66 | @use "sass:math"; 67 | @use "sass:list"; 68 | 69 | $root-font-size: 16px !default; 70 | $base-font-size: $root-font-size !default; 71 | 72 | 73 | // Absolute lengths 74 | @function px($input){ @return to-unit(px, $input); } 75 | @function pt($input){ @return to-unit(pt, $input); } 76 | @function pc($input){ @return to-unit(pc, $input); } 77 | @function in($input){ @return to-unit(in, $input); } 78 | @function mm($input){ @return to-unit(mm, $input); } 79 | @function cm($input){ @return to-unit(cm, $input); } 80 | 81 | // Angles 82 | @function deg($input){ @return to-unit(deg, $input); } 83 | @function rad($input){ @return to-unit(rad, $input); } 84 | @function grad($input){ @return to-unit(grad, $input); } 85 | @function turn($input){ @return to-unit(turn, $input); } 86 | 87 | // Resolution 88 | @function dpi($input){ @return to-unit(dpi, $input); } 89 | @function dpcm($input){ @return to-unit(dpcm, $input); } 90 | @function dppx($input){ @return to-unit(dppx, $input); } 91 | 92 | // Time 93 | @function ms($input){ @return to-unit(ms, $input); } 94 | @function s($input){ @return to-unit(s, $input); } 95 | 96 | // Frequencies 97 | @function hz($input){ @return to-unit(hz, $input);} 98 | @function khz($input){ @return to-unit(khz, $input); } 99 | 100 | // Relative lengths 101 | @function em($input...){ 102 | $em: to-unit(em, nth($input,1)); 103 | // Adjust for compounds (visual size) 104 | @if length($input) > 1 { 105 | @for $i from 2 through length($input){ 106 | $em: math.div($em, num(em(nth($input,$i)))); 107 | } 108 | } 109 | @return $em; 110 | } 111 | @function rem($input){ @return to-unit(rem, $input); } 112 | 113 | // Inconvertible relative lengths – depends on font 114 | @function ex($input){ @return to-unit(ex, $input); } 115 | @function ch($input){ @return to-unit(ch, $input); } 116 | 117 | // Viewport 118 | @function vw($input){ @return to-unit(vw, $input); } 119 | @function vh($input){ @return to-unit(vh, $input); } 120 | @function vmin($input){ @return to-unit(vmin, $input); } 121 | @function vmax($input){ @return to-unit(vmax, $input); } 122 | 123 | // Strings and numbers 124 | @function str($input){ @return #{$input}; } 125 | @function num($input){ 126 | @if type-of($input) != number { 127 | @error 'Could not convert `#{$input}` - must be `type-of number`'; 128 | @return null; 129 | } 130 | @return math.div($input, $input*0+1); 131 | } 132 | @function int($input){ 133 | $num: num($input); 134 | @return if($num<0, ceil($num), floor($num)); 135 | } 136 | @function uint($input){ @return abs(int($input)); } 137 | 138 | // Aliases 139 | @function string($input){ @return str($input);} 140 | @function number($input){ @return num($input);} 141 | 142 | 143 | // Conversion function 144 | @function to-unit($unit, $input){ 145 | // Test against valid CSS units 146 | $to-unit: map-get(( 147 | px: 0px, pt: 0pt, pc: 0pc, in: 0in, mm: 0mm, cm: 0cm, // absolute length 148 | em: 0em, rem: 0rem, ch: 0ch, ex: 0ex, // relative length - font based 149 | vw: 0vw, vh: 0vh, vmin: 0vmin, vmax: 0vmax, // relative length - viewport based 150 | deg: 0deg, turn: 0turn, grad: 0grad, rad: 0rad, // angle 151 | s: 0s, ms: 0ms, // time 152 | hz: 0Hz, khz: 0kHz, // frequency 153 | dpi: 0dpi, dpcm: 0dpcm, dppx: 0dppx, // resolution 154 | pct: 0%, percent: 0%, num: 0, number: 0 // percent or number 155 | ), $unit); 156 | 157 | // Error handling – wrong $unit 158 | // Incomparable units are caught in convertion 159 | @if not $to-unit { 160 | @error 'Could not convert to `#{$unit}` – must be a valid CSS unit'; 161 | @return null; 162 | } 163 | 164 | // Number/incomparable conversion 165 | @if index(num number ex ch vw vh vmin vmax, $unit) { 166 | $value: num($input); 167 | } 168 | 169 | // EM convertion using px as base 170 | @if index(em, unit($input)) { 171 | $input: 0px + math.div(num($input) * $base-font-size, 1px); 172 | } 173 | @if index(em, $unit) and not unitless($input) { 174 | $input: 0px + px($input); 175 | $input: math.div(num($input) * 1px, $base-font-size); 176 | } 177 | 178 | // REM convertion using px as base 179 | @if index(rem, unit($input)) { 180 | $input: 0px + math.div(num($input) * $root-font-size, 1px); 181 | } 182 | @if index(rem, $unit) and not unitless($input) { 183 | $input: 0px + $input; 184 | $input: math.div(num($input) * 1px, $root-font-size); 185 | } 186 | 187 | // Bug fix – resolution units seems to be flipped 188 | @if index(dpi dpcm dppx, $unit){ 189 | $units: (dppx: 0dppx, dpcm: 0dpcm, dpi: 0dpi); 190 | $input-unit: map-get($units, unit($input)); 191 | $input: if(1dppx < 95dpi,num($input-unit + (num($input) + $to-unit)),$input); 192 | } 193 | 194 | // Convert 195 | @return $to-unit + $input; 196 | } 197 | 198 | // Convert number to ratio (fraction) 199 | // ratio(1.7777778) => 16/9 200 | @function ratio($x, $y: null){ 201 | @if not $y { 202 | $n: $x; $y: 1; 203 | @while $y < 10 { 204 | $x: $n * 10 - ((10 - $y) * $n); 205 | @if $x == round($x){ @return list.slash($x, $y); } 206 | @else { $y: $y + 1; } 207 | } 208 | $x: round($n * 1000000); $y: 1000000; 209 | @while $x % 10 == 0 { $x: $x*0.1; $y: $y*0.1; } 210 | @while $x % 5 == 0 { $x: $x*0.2; $y: $y*0.2; } 211 | @while $x % 2 == 0 { $x: $x*0.5; $y: $y*0.5; } 212 | @return list.slash($x, $y); 213 | } 214 | @else if $x == round($x) and $y == round($y){ @return list.slash($x, $y); } 215 | @warn 'X and Y must be integers'; @return false; 216 | } 217 | --------------------------------------------------------------------------------