douchendan0040 2014-10-06 14:04
浏览 89
已采纳

PHP将24位颜色转换为4位颜色

Background, I am converting images to ascii art. This works perfectly and even works with 24-bit color, converting the colors to the right rgb values. However, I now want to render the ascii art in 4-bit color palette rather than 24-bit.

How do I convert 24-bit colors to 4-bit with PHP?

More specifically, I have the standard IRC color pallet which I need to convert any given Hexidecimal or RGB value to. It is preferred that the colors match as best as possible when converted to the 4-bit color.

Other ideas I have had on this are to convert the image itself to a 4-bit palette (using GD, which is what I use to read in the colors right now) before trying to grab colors off of it. And another idea might be to define a color range for each of the following color and just check that the given 24-bit color is in the range, however I wouldn't know how to get the ranges for all colors into that palette.

enter image description here

  • 写回答

4条回答 默认 最新

  • doucuo4413 2014-10-08 01:10
    关注

    In the end, despite the wonderful suggestions surrounding imagemagick I found a good solution using straight php. I was able to calculate the closest color through the use of delta E 2000 with a modified version of php-color-difference library found on github, here is my fork: https://github.com/nalipaz/php-color-difference

    The pertinent example is:

    <?php
    include('lib/color_difference.class.php');
    
    $palette = array(
      '00' => array(255, 255, 255),
      '01' => array(0, 0, 0),
      '02' => array(0, 0, 139),
      '03' => array(0, 128, 0),
      '04' => array(255, 0, 0),
      '05' => array(139, 0, 0),
      '06' => array(128, 0, 128),
      '07' => array(255, 165, 0),
      '08' => array(255, 255, 0),
      '09' => array(50, 205, 50),
      '10' => array(0, 128, 128),
      '11' => array(173, 216, 230),
      '12' => array(0, 0, 255),
      '13' => array(255, 105, 180),
      '14' => array(128, 128, 128),
      '15' => array(211, 211, 211),
    );
    
    $color_rgb = array(255, 255, 128);
    $color_delta_e = new color_difference($color_rgb);
    $match_index = $color_delta_e->getClosestMatch($palette);
    $color = $palette[$match_index];
    

    I am pretty happy with this solution and smaller amount of overhead. Thanks for the suggestions guys.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 RHEL8.10安装文件
  • ¥25 实时时钟,测温,模拟量采集,超声波测距,四种在数码管上的显示通过独立按键S4依次切换
  • ¥50 Linq更新数据导致数据库崩溃
  • ¥15 使用vue3+springboot+elementplus开发web小项目,用easyexcel导出后台查询到的数据。postman测试下载测试成功但是浏览器不能触发下载
  • ¥15 关于#r语言#的问题:单细胞数据处理
  • ¥15 我主页资源的代码要如何导入JAVAEE里面使其可以运行
  • ¥20 如何在linux centos7中,搭建DVWA?
  • ¥20 更改手机应用的音频输入/推荐其他方案
  • ¥15 数据库 'LibraryManage' 已存在。请选择其他数据库名称。
  • ¥20 windows毛玻璃窗口缩放延时问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部