harraps

radiant.js

Jan 27th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // radiant.js v5.1
  2. // Created by harraps
  3. // This script is free for use for anyone who wants to use it
  4. // You might want to compress it to optimize the reactivity of your web pages
  5. // to use it, put the class rad-0-fg for foreground effects or rad-0-bg for background effects
  6. // to select a color, use implicit radiant angle: 0, 1, 2, 3,... ( * pi/6 )
  7.  
  8. var radiant = {};
  9.  
  10. radiant.fps        = 20;   // frame per seconds, the number of times the color will be updated
  11. radiant.lightness  = 128;  // the value from witch we go upper or lower (color channels go from 0 to 255)
  12. radiant.saturation = 100;  // how much do we go up or down ( in that case 128-100=28>0 , 128+100=228<255 )
  13. radiant.increment  = 0.01; // the value to increment the hue
  14.  
  15. (function () {
  16.    
  17.     // this function allow us to shorten the document.getElementsByClassName() function name
  18.     function collect( c ){
  19.         return document.getElementsByClassName( c );
  20.     }
  21.    
  22.     var div_fg = [
  23.         collect("rad-0-fg"),
  24.         collect("rad-1-fg"),
  25.         collect("rad-2-fg"),
  26.         collect("rad-3-fg"),
  27.         collect("rad-4-fg"),
  28.         collect("rad-5-fg"),
  29.         collect("rad-6-fg"),
  30.         collect("rad-7-fg"),
  31.         collect("rad-8-fg"),
  32.         collect("rad-9-fg"),
  33.         collect("rad-10-fg"),
  34.         collect("rad-11-fg")
  35.     ];
  36.     var div_bg = [
  37.         collect("rad-0-bg"),
  38.         collect("rad-1-bg"),
  39.         collect("rad-2-bg"),
  40.         collect("rad-3-bg"),
  41.         collect("rad-4-bg"),
  42.         collect("rad-5-bg"),
  43.         collect("rad-6-bg"),
  44.         collect("rad-7-bg"),
  45.         collect("rad-8-bg"),
  46.         collect("rad-9-bg"),
  47.         collect("rad-10-bg"),
  48.         collect("rad-11-bg")
  49.     ];
  50.  
  51.     var PI         = 3.1415926535897932384626433832795; // the value of PI
  52.     var PI2        = PI * 2;        // twice the value of PI
  53.     var PI2_THIRD1 = PI2 / 3;       // 1/3 of 2PI
  54.     var PI2_THIRD2 = (PI2 * 2) / 3; // 2/3 of 2PI
  55.     var PI_SIXTH   = PI / 6;        // a sixth of the value of PI
  56.    
  57.     var hue = Math.random() * PI2; // the hue of the color
  58.     var red, green, blue, i, j;    // we declare the RGB channels and the i and j indexes
  59.     var length = 12;               // length of the array of colors
  60.     var colors = [];               // we want to store 12 colors
  61.  
  62.     // This function generate a color in hexadecimal from RGB channels
  63.     function RGB2Color(r,g,b){
  64.         return '#' + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
  65.     }
  66.  
  67.     // This function generate a hexadecimal sequence from a byte
  68.     function byte2Hex(n){
  69.         var nybHexString = "0123456789ABCDEF";
  70.         return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
  71.     }
  72.  
  73.     // This function will generate a hexadecimal color String for the given frequency
  74.     function hue2Color(f){
  75.         // then we get the rgb value of the color from the frequency
  76.         red   = Math.sin(f             ) * radiant.saturation + radiant.lightness;
  77.         green = Math.sin(f + PI2_THIRD1) * radiant.saturation + radiant.lightness;
  78.         blue  = Math.sin(f + PI2_THIRD2) * radiant.saturation + radiant.lightness;
  79.         return RGB2Color(red, green, blue);
  80.     }
  81.  
  82.     // this function will be repeated 20 times per second
  83.     window.setInterval(function() {
  84.         // first we increment the frequency but keep it under two PI
  85.         hue = (hue + radiant.increment) % PI2;
  86.        
  87.         for( i=0; i<length; ++i ){
  88.             // for each of the 12 colors we change it a little
  89.             colors[i] = hue2Color( hue + i * PI_SIXTH );
  90.            
  91.             // for each elements of our array, we set its color to the new color we've generated
  92.             for( j=0; j<div_fg[i].length; ++j ){
  93.                 div_fg[i][j].style.color = colors[i];
  94.             }
  95.             for( j=0; j<div_bg[i].length; ++j ){
  96.                 div_bg[i][j].style.backgroundColor = colors[i];
  97.             }
  98.         }
  99.        
  100.     }, 1000 / radiant.fps );
  101. })();
Advertisement
Add Comment
Please, Sign In to add comment