├── .gitignore ├── README.md ├── config.rb ├── css ├── _helpers.scss ├── style.css └── style.scss ├── img ├── randomstringtocsscolor.ico └── randomstringtocsscolor_200.jpg ├── index.html └── js └── main.js /.gitignore: -------------------------------------------------------------------------------- 1 | .sass-cache/ 2 | sftp-config.json 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | randomstringtocsscolor 2 | -------------------------------------------------------------------------------- /config.rb: -------------------------------------------------------------------------------- 1 | # Require any additional compass plugins here. 2 | 3 | # Set this to the root of your project when deployed: 4 | http_path = "/" 5 | css_dir = "css" 6 | sass_dir = "css" 7 | images_dir = "img" 8 | javascripts_dir = "js" 9 | 10 | # You can select your preferred output style here (can be overridden via the command line): 11 | # output_style = :expanded or :nested or :compact or :compressed 12 | output_style = :compressed 13 | 14 | # To enable relative paths to assets via compass helper functions. Uncomment: 15 | # relative_assets = true 16 | 17 | # To disable debugging comments that display the original location of your selectors. Uncomment: 18 | line_comments = false 19 | 20 | 21 | # If you prefer the indented syntax, you might want to regenerate this 22 | # project again passing --syntax sass, or you can uncomment this: 23 | # preferred_syntax = :sass 24 | # and then run: 25 | # sass-convert -R --from scss --to sass sass scss && rm -rf sass && mv scss sass 26 | -------------------------------------------------------------------------------- /css/_helpers.scss: -------------------------------------------------------------------------------- 1 | @mixin breakpoint($point) { 2 | @if $point == papa-bear { 3 | @media (max-width: 79em) { @content; } 4 | } 5 | @if $point == mama-bear { 6 | @media (max-width: 66em) { @content; } 7 | } 8 | @if $point == baby-bear { 9 | @media (max-width: 44em) { @content; } 10 | } 11 | } -------------------------------------------------------------------------------- /css/style.css: -------------------------------------------------------------------------------- 1 | *{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}body{margin:1em;font:1em sans-serif;color:#fff;-webkit-transition:background-color 0.15s ease-in-out;-moz-transition:background-color 0.15s ease-in-out;-o-transition:background-color 0.15s ease-in-out;transition:background-color 0.15s ease-in-out}@media (max-width: 44em){body{margin:1em 0}}a{text-decoration:none;color:#fff;padding-bottom:.5em;border-bottom:.15em solid #fff;-webkit-transition:color 0.25s ease-in-out,border-bottom 0.25s ease-in-out;-moz-transition:color 0.25s ease-in-out,border-bottom 0.25s ease-in-out;-o-transition:color 0.25s ease-in-out,border-bottom 0.25s ease-in-out;transition:color 0.25s ease-in-out,border-bottom 0.25s ease-in-out}@media (max-width: 44em){a{line-height:1.65em;padding-bottom:.15em}}a:hover{color:#000;border-bottom:.15em solid #000}h1{font-size:2em}article{width:80%;margin:5em auto 0 auto}@media (max-width: 44em){article{width:100%;margin:2em auto 0 auto;padding:0 .5em}}article input{width:100%;border:none;font-size:4em;line-height:1em;margin:0 0 1em 0;padding:.15em;background:rgba(0,0,0,0.7);color:#fff;outline:0;-webkit-transition:outline 0.3s ease-in-out,box-shadow 0.2s ease-in-out;-moz-transition:outline 0.3s ease-in-out,box-shadow 0.2s ease-in-out;-o-transition:outline 0.3s ease-in-out,box-shadow 0.2s ease-in-out;transition:outline 0.3s ease-in-out,box-shadow 0.2s ease-in-out}@media (max-width: 44em){article input{font-size:2em}}article input:focus{outline:0.15em solid rgba(0,0,0,0.2);box-shadow:inset 0 0 0.25em rgba(0,0,0,0.6)}article .output{float:left;width:50%;text-align:center;color:#fff;padding:2em}article section{border-top:.25em solid #fff;padding-top:1.5em;margin:5em 0 0 0}article section ol{list-style:decimal}article section ol li{line-height:2em}article section p{margin:.5em 0 2em 0} 2 | -------------------------------------------------------------------------------- /css/style.scss: -------------------------------------------------------------------------------- 1 | @import "compass/css3"; 2 | @import "helpers"; 3 | 4 | * { 5 | @include box-sizing(border-box); 6 | } 7 | 8 | body { 9 | margin:1em; 10 | font:1em sans-serif; 11 | color:#fff; 12 | @include transition(background-color .15s ease-in-out); 13 | 14 | @include breakpoint(baby-bear) { 15 | margin: 1em 0; 16 | } 17 | } 18 | 19 | a { 20 | text-decoration:none; 21 | color:#fff; 22 | padding-bottom:.5em; 23 | border-bottom:.15em solid #fff; 24 | @include transition(color .25s ease-in-out, border-bottom .25s ease-in-out); 25 | 26 | @include breakpoint(baby-bear) { 27 | line-height:1.65em; 28 | padding-bottom:.15em; 29 | } 30 | 31 | &:hover { 32 | color:#000; 33 | border-bottom:.15em solid #000; 34 | } 35 | } 36 | 37 | h1 { 38 | font-size:2em; 39 | } 40 | 41 | article { 42 | width:80%; 43 | margin:5em auto 0 auto; 44 | 45 | @include breakpoint(baby-bear) { 46 | width:100%; 47 | margin: 2em auto 0 auto; 48 | padding:0 .5em; 49 | } 50 | 51 | input { 52 | width:100%; 53 | border:none; 54 | font-size:4em; 55 | line-height:1em; 56 | margin: 0 0 1em 0; 57 | padding:.15em; 58 | background:rgba(0, 0, 0, .7); 59 | color:#fff; 60 | outline:0; 61 | @include transition(outline .3s ease-in-out, box-shadow .2s ease-in-out); 62 | 63 | @include breakpoint(baby-bear) { 64 | font-size:2em; 65 | } 66 | 67 | &:focus { 68 | outline:.15em solid rgba(0, 0, 0, .2); 69 | box-shadow:inset 0 0 .25em rgba(0, 0, 0, .6); 70 | } 71 | } 72 | 73 | .output { 74 | float:left; 75 | width:50%; 76 | text-align:center; 77 | color:#fff; 78 | padding:2em; 79 | } 80 | 81 | section { 82 | border-top:.25em solid #fff; 83 | padding-top:1.5em; 84 | margin:5em 0 0 0; 85 | 86 | ol { 87 | list-style:decimal; 88 | 89 | li { 90 | line-height:2em; 91 | } 92 | } 93 | 94 | p { 95 | margin:.5em 0 2em 0; 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /img/randomstringtocsscolor.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TimPietrusky/randomstringtocsscolor/3565ded1ac0cb7b49f6ddf06a35a38655cb4cd5f/img/randomstringtocsscolor.ico -------------------------------------------------------------------------------- /img/randomstringtocsscolor_200.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TimPietrusky/randomstringtocsscolor/3565ded1ac0cb7b49f6ddf06a35a38655cb4cd5f/img/randomstringtocsscolor_200.jpg -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | random string to css color 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 |
26 |
27 | 28 |
29 |

random string to css color

30 |

31 | 2013 by @TimPietrusky

32 | 33 |

34 | Fork me on GitHub

35 | 36 |

37 | randomstringtocsscolor.com

38 |
39 | 40 | 41 |
42 |

How does this work?

43 |
    44 |
  1. Change each non-hex character to a 0.
  2. 45 |
  3. Add 0's to the string until its length is a multiple of 3.
  4. 46 |
  5. Divide string into 3 equal parts.
  6. 47 |
  7. While the length of the sub-strings is greater than 2, and all three of the sub-strings begin with a 0, remove the leading 0s from each string.
  8. 48 |
  9. If the length of the sub-strings is still greater than 2, then truncate each substring to 2 characters.
  10. 49 |
50 | 51 | Source: Why do weird things in font color attribute produce real colors? 52 |
53 |
54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /js/main.js: -------------------------------------------------------------------------------- 1 | /** 2 | Convert a random string to css color 3 | 4 | randomstringtocsscolor.com 5 | 6 | This implementation is very simple :D 7 | 8 | # 2013 by Tim Pietrusky 9 | # timpietrusky.com 10 | **/ 11 | 12 | var output = document.querySelector('.output'), 13 | output_rgba = document.querySelector('.output.rgba'), 14 | input = document.querySelector('input'), 15 | body = document.body; 16 | 17 | // Listen to keyup 18 | input.addEventListener('keyup', function(e) { 19 | var value = this.value, 20 | result = ""; 21 | 22 | try { 23 | value = value.split(''); 24 | 25 | // Filter non hex values 26 | for (var i = 0; i < value.length; i++) { 27 | var val = value[i]; 28 | 29 | if (!/^[0-9A-F]{1}$/i.test(val)) { 30 | val = 0; 31 | } 32 | 33 | result += val; 34 | } 35 | 36 | // Multiple of 3 37 | if (result.length % 3) { 38 | result += Array((3 - result.length % 3) + 1).join("0"); 39 | } 40 | 41 | // Multiple of 6 42 | if (result.length % 6) { 43 | // result += Array((6 - result.length % 6) + 1).join("0"); 44 | } 45 | 46 | // Split in 3 groups with equal size 47 | var regexp = new RegExp("([A-Z0-9]{"+result.length / 3+"})", "i"); 48 | result = result.split(regexp); 49 | 50 | // Remove first 0 (if there is one at first postion of every group 51 | if (result[1].length > 2) { 52 | if (result[1].charAt(0) == result[3].charAt(0) == result[5].charAt(0) == 0) { 53 | result[1] = result[1].substr(1); 54 | result[3] = result[3].substr(1); 55 | result[5] = result[5].substr(1); 56 | } 57 | } 58 | 59 | // Truncate (first 2 chars stay, the rest gets deleted) 60 | result[1] = result[1].slice(0, 2); 61 | result[3] = result[3].slice(0, 2); 62 | result[5] = result[5].slice(0, 2); 63 | 64 | // Add element if color consists of just 1 char per color 65 | if (result[1].length == 1) { 66 | result[1] += result[1]; 67 | result[3] += result[3]; 68 | result[5] += result[5]; 69 | } 70 | 71 | // Output 72 | output.innerText = "#"+result[1] + result[3] + result[5]; 73 | 74 | // Convert to RGB 75 | r = parseInt(result[1], 16); 76 | g = parseInt(result[3], 16); 77 | b = parseInt(result[5], 16); 78 | 79 | body.setAttribute('style', 'background-color:rgba('+r+','+g+','+b+',.8)'); 80 | output_rgba.innerText = 'rgba('+r+','+g+','+b+',.8)'; 81 | 82 | } catch(e) { 83 | body.setAttribute('style', 'background-color:none'); 84 | output.innerText = output_rgba.innerText = ""; 85 | } 86 | }); --------------------------------------------------------------------------------