Created
October 17, 2014 21:48
-
-
Save m93a/942481ef7fbf079c137f to your computer and use it in GitHub Desktop.
Konvertor RGB barev
This file contains 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
<!document html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title>Převod barev</title> | |
<style> | |
input{ | |
font-family: Courier New; | |
} | |
div input{ | |
width: 100px; | |
} | |
</style> | |
</head> | |
<body> | |
<div> | |
<input id=red placeholder=R /> | |
<input id=green placeholder=G /> | |
<input id=blue placeholder=B /> | |
</div> | |
<input id=hex placeholder=#hex /> | |
<script> | |
var r = document.getElementById('red' ); //input pro červenou | |
var g = document.getElementById('green'); //input pro zelenou | |
var b = document.getElementById('blue' ); //input pro modrou | |
var hex = document.getElementById('hex'); //input pro hexakód | |
function pad(str,length,char){ | |
//Tahle funkce doplní řetězec tak, aby měl správnou délku | |
while(str.length < length){ | |
str = char+str; | |
} | |
return str; | |
} | |
function inputChange(e){ | |
//Tahle funkce je spuštěna vždycky, když něco napíšeš do inputu | |
if(e.target == hex){ //pokud byl změněn hexakód | |
//Převeď na malá písmena | |
hex.value = hex.value.toLowerCase(); | |
//Pokud hexakód nezačíná mřížkou, hodnota je neplatná, | |
//Vymaž tedy číselné hodnoty RGB inputů a ukonči funkci | |
if(hex.value[0] != "#"){ | |
r.value = ""; | |
g.value = ""; | |
b.value = ""; | |
return; | |
} | |
//Získej části hexakódu odpovídající barvám | |
//substr(a,b) získá z řetězce b znaků, počínaje a-tým znakem v pořadí | |
var red = hex.value.substr(1,2); | |
var green = hex.value.substr(3,2); | |
var blue = hex.value.substr(5,2); | |
//Převeď číslo z šestnáctkové soustavy do desítkové | |
red = parseInt(red, 16).toString(10); | |
green = parseInt(green,16).toString(10); | |
blue = parseInt(blue, 16).toString(10); | |
//Pokud jsou vypočítané hodnoty neplatné, nastav | |
//prázdný řetězec, jinak nastav vypočítané hodnoty | |
//Operátor a ? b : c znamená pokud a je pravda, | |
//vrať hodnotu b, jinak vrať hodnotu c | |
r.value = isNaN(red ) ? "" : red; | |
g.value = isNaN(green) ? "" : green; | |
b.value = isNaN(blue ) ? "" : blue; | |
}else{ //pokud byla změněna čísla | |
//Získej číselné hodnoty RGB | |
//Očekává desítkovou soustavu | |
var red = parseInt(r.value,10); | |
var green = parseInt(g.value,10); | |
var blue = parseInt(b.value,10); | |
//Nastav vypočítaný hexakód | |
//Kód začíná znakem "#", potom je červená, zelená a modrá | |
//toString(16) převede číslo do šestnáctkové soustavy | |
//pad( něco, 2, "0") zajistí, že je každá část dlouhá 2 znaky | |
hex.value = "#" + pad( red.toString(16), 2, "0") | |
+ pad( green.toString(16), 2, "0") | |
+ pad( blue.toString(16), 2, "0"); | |
if(hex.value.length != 7){ | |
//Když délka neodpovídá (tzn. někde je chybná hodnota), | |
//Nastav prázdnou hodnotu | |
hex.value = ""; | |
} | |
} | |
} | |
//Nastav "odposlouchávání" změn hodnot inputů | |
r.oninput = inputChange; | |
g.oninput = inputChange; | |
b.oninput = inputChange; | |
hex.oninput = inputChange; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment