index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * FeHelper 多维度工具组合
  3. */
  4. new Vue({
  5. el: '#pageContainer',
  6. data: {
  7. radix: {
  8. fromArray: [2, 4, 8, 10, 16],
  9. fromSelected: 10,
  10. toArray: [2, 4, 8, 10, 16],
  11. toSelected: 16,
  12. srcValue: 100,
  13. rstValue: 0
  14. },
  15. color: {
  16. fromHEX: '#43ad7f7f',
  17. toRGB: '',
  18. fromRGB: 'rgba(190,20,128,0.5)',
  19. toHEX: ''
  20. }
  21. },
  22. mounted: function () {
  23. jQuery('#tabs').tabs();
  24. // 进制转换的初始化
  25. this.radixConvert();
  26. // 初始化颜色转换
  27. this.colorHexToRgb();
  28. this.colorRgbToHex();
  29. },
  30. methods: {
  31. getId: (type, id) => [type, id].join('_'),
  32. radixRadioClicked: function (type, n) {
  33. if (type === 1) {
  34. this.radix.fromSelected = n;
  35. } else {
  36. this.radix.toSelected = n;
  37. }
  38. this.radixConvert();
  39. },
  40. radixConvert: function () {
  41. this.$nextTick(() => {
  42. this.radix.rstValue = parseInt(this.radix.srcValue, this.radix.fromSelected).toString(this.radix.toSelected);
  43. });
  44. },
  45. colorHexToRgb: function () {
  46. let hex = this.color.fromHEX.trim().replace(/^#/, '');
  47. let rgb = [];
  48. switch (hex.length) {
  49. case 3:
  50. case 4:
  51. rgb.push(parseInt(hex[0] + '' + hex[0], 16).toString(10)); // r
  52. rgb.push(parseInt(hex[1] + '' + hex[1], 16).toString(10)); // g
  53. rgb.push(parseInt(hex[2] + '' + hex[2], 16).toString(10)); // b
  54. hex.length === 4 && rgb.push((parseInt(parseInt(hex[3] + '' + hex[3], 16).toString(10)) / 256).toFixed(2)); // a
  55. break;
  56. case 6:
  57. case 8:
  58. rgb.push(parseInt(hex[0] + '' + hex[1], 16).toString(10)); // r
  59. rgb.push(parseInt(hex[2] + '' + hex[3], 16).toString(10)); // g
  60. rgb.push(parseInt(hex[4] + '' + hex[5], 16).toString(10)); // b
  61. hex.length === 8 && rgb.push((parseInt(parseInt(hex[6] + '' + hex[7], 16).toString(10)) / 256).toFixed(2)); // a
  62. break;
  63. }
  64. if (rgb.length === 3) {
  65. this.color.toRGB = 'rgb(' + rgb.join(', ') + ')';
  66. } else if (rgb.length === 4) {
  67. this.color.toRGB = 'rgba(' + rgb.join(', ') + ')';
  68. } else {
  69. this.color.toRGB = '';
  70. }
  71. },
  72. colorRgbToHex: function () {
  73. let rgb = this.color.fromRGB.trim().replace(/\s+/g, '').replace(/[^\d,\.]/g, '').split(',').filter(n => {
  74. return n.length && parseInt(n, 10) <= 255;
  75. });
  76. let hex = [];
  77. if (rgb.length === 3 || rgb.length === 4) {
  78. hex.push(parseInt(rgb[0], 10).toString(16).padStart(2, '0')); // r
  79. hex.push(parseInt(rgb[1], 10).toString(16).padStart(2, '0')); // g
  80. hex.push(parseInt(rgb[2], 10).toString(16).padStart(2, '0')); // b
  81. rgb.length === 4 && hex.push(Math.floor(parseFloat(rgb[3], 10) * 256).toString(16).padStart(2, '0')); // a
  82. }
  83. if (hex.length) {
  84. this.color.toHEX = '#' + hex.join('');
  85. } else {
  86. this.color.toHEX = '';
  87. }
  88. }
  89. }
  90. });