Utils.as

  1. /**
  2.  * Métodos genéricos
  3.  *
  4.  * ProInf.net, 2008
  5.  * GNU General Public License: http://creativecommons.org/licenses/GPL/2.0/deed.es
  6.  *
  7.  */
  8. class Utils {
  9.  
  10. //////////////////////////////////////////////////
  11. // Funciones de comprobación
  12.  
  13. /** Undefined to empty string */
  14. public static function clearUndefined (value, valueIfUndefined) {
  15. if (value == undefined) return valueIfUndefined;
  16. return value;
  17. }
  18.  
  19. //////////////////////////////////////////////////
  20. // Funciones de texto
  21.  
  22. public static function startsWith(str:String, prefix:String):Boolean {
  23. return str.slice(0, prefix.length) == prefix;
  24. }
  25.  
  26. public static function endsWith(str:String, sufix:String):Boolean {
  27. return str.slice(-sufix.length, str.length) == sufix;
  28. }
  29. public static function contains(str:String, test:String):Boolean {
  30. return str.indexOf(test) != -1;
  31. }
  32.  
  33. //////////////////////////////////////////////////
  34. // Funciones de color
  35.  
  36. public static function setTint(mc:MovieClip, rgb:Number, alfa:Number):Void {
  37. // http://www.actionscripts.org/forums/showthread.php3?p=289593
  38.  
  39. var tc:Object = new Object();
  40. tc.ra = tc.ga = tc.ba = 100-alfa;
  41. var ratio:Number = alfa/100;
  42. var red = 0xFF & rgb;
  43. var green = 0xFF & (rgb >> 8);
  44. var blue = 0xFF & (rgb >> 16);
  45. tc.rb = red * ratio;
  46. tc.gb = green * ratio;
  47. tc.bb = blue * ratio;
  48.  
  49. var color:Color = new Color(mc);
  50. color.setTransform (tc);
  51. }
  52.  
  53. /** Convierte "#fa89c6" en 0xFA89C6 */
  54. public static function parseColor(strColor:String):Number {
  55. //http://flash-creations.com/notes/asclass_color.php
  56. if (strColor.substr(0, 1) == "#") {
  57. strColor = strColor.substr(1,6);
  58. return parseInt(strColor, 16);
  59. }
  60. else {
  61. return NaN;
  62. }
  63. }
  64.  
  65.  
  66. //////////////////////////////////////////////////
  67. // Autocomprobación
  68.  
  69. public static function test() {
  70. //Console.show( "Barcelona".slice(0,2) );
  71. //Console.show( "Barcelona".slice(-2,"Barcelona".length) );
  72. Console.test( startsWith("http://www.proinf.net/","http://"), true);
  73. Console.test( endsWith("pelicula.swf", ".swf"), true);
  74. }
  75.  
  76.  
  77. }

Proinf.net