zoukankan      html  css  js  c++  java
  • js 计算两个颜色之间的渐变色值 10个色值

    https://www.zhihu.com/question/38869928

    随手写的,轻喷

    Changelog:

      • 应评论区要求支持 #000 这样的三位 hex color
      • 应评论区要求支持 gamma correction。
    作者:「已注销」
    链接:https://www.zhihu.com/question/38869928/answer/78527903
    来源:知乎
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    
    // convert #hex notation to rgb array
    var parseColor = function (hexStr) {
      return hexStr.length === 4 ? hexStr.substr(1).split('').map(function (s) { return 0x11 * parseInt(s, 16); }) : [hexStr.substr(1, 2), hexStr.substr(3, 2), hexStr.substr(5, 2)].map(function (s) { return parseInt(s, 16); })
    };
    
    // zero-pad 1 digit to 2
    var pad = function (s) {
      return (s.length === 1) ? '0' + s : s;
    };
    
    var gradientColors = function (start, end, steps, gamma) {
      var i, j, ms, me, output = [], so = [];
      gamma = gamma || 1;
      var normalize = function (channel) {
        return Math.pow(channel / 255, gamma);
      };
      start = parseColor(start).map(normalize);
      end = parseColor(end).map(normalize);
      for (i = 0; i < steps; i++) {
        ms = i / (steps - 1);
        me = 1 - ms;
        for (j = 0; j < 3; j++) {
          so[j] = pad(Math.round(Math.pow(start[j] * me + end[j] * ms, 1 / gamma) * 255).toString(16));
        }
        output.push('#' + so.join(''));
      }
      return output;
    };
    
    // try if it works
    console.log(gradientColors('#00ff00', '#ff0000', 100));
    
    // 泥萌的新需求
    console.log(gradientColors('#000', '#fff', 100, 2.2));

    另canvas做法

    http://blog.sina.com.cn/s/blog_a1a495090102vqms.html

  • 相关阅读:
    mongoDB 小练习
    pymongo 操作
    mongoDB 大文件存储方案, JS 支持展示
    mongoDB 固定集合
    mongoDB 聚合操作
    mongoDB 索引
    mongoDB 其他数据类型
    mongoDB 文档操作_删
    mongoDB 文档操作_改
    【探讨】linux环境,执行重启了php后php.ini依然不生效
  • 原文地址:https://www.cnblogs.com/dhjy123/p/13492460.html
Copyright © 2011-2022 走看看