Skip to content

Instantly share code, notes, and snippets.

@jonas-kgomo
Last active February 3, 2020 09:21
Show Gist options
  • Save jonas-kgomo/71eba0ae8ba6025b78dc45772a0ae7ca to your computer and use it in GitHub Desktop.
Save jonas-kgomo/71eba0ae8ba6025b78dc45772a0ae7ca to your computer and use it in GitHub Desktop.
A pure function parsing decimal to heximal and vice-versa

Method 1

This script will help us convert colours from heximal to decimal values.

function HextoDec(h) {  
	var dec = parseInt(h, 16); // 16 to 10 base 
	// 16777215 
	return dec 
}

// HexToDec("FFFFFF") = 0

function DectoHex(d) {

//    convert number to 32-bit String, number converts the object to a number 
    var hex = Number(d).toString(16).toUpperCase().padStart(2, '0');
  return hex;
}
// DecToHex(16)= 10

Vanilla JS

https://gist.github.com/erwinw/79b4e6dbc66b5e148752

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment