PHP将24位颜色转换为4位 [英] PHP convert 24-bit colors to 4-bit

查看:213
本文介绍了PHP将24位颜色转换为4位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景,我将图像转换为ascii艺术。这工作完美,甚至工程与24位颜色,将颜色转换为正确的rgb值。然而,我现在想要渲染ascii艺术在4位调色板而不是24位。



如何将24位颜色转换为4位PHP?



更具体地说,我有标准的IRC颜色托盘,我需要转换任何给定的十六进制或RGB值。



其他的想法,我已经在这是将图像本身转换为一个4位调色板(使用GD,这是我现在使用的颜色读取),然后试图抓住它的颜色。另一个想法可能是为每个下面的颜色定义一个颜色范围,只是检查给定的24位颜色是否在范围内,但是我不知道如何获得所有颜色的范围到该调色板。 / p>

>

解决方案

最后,尽管围绕imagemagick的精彩建议,我发现了一个很好的解决方案使用直php。我能够通过使用delta E 2000与github上找到的php-color-difference库的修改版本来计算最接近的颜色,这里是我的fork: https://github.com/nalipaz/php-color-difference



相关示例是:

 <?php 
include('lib / color_difference.class.php');

$ palette = array(
'00'=> array(255,255,255),
'01'=& ,
'02'=> array(0,0,139),
'03'=> array(0,128,0),
'04'=& (255,0,0),
'05'=>数组(139,0,0),
'06'=> array(128,0,128),
'07'=> array(255,165,0),
'08'=> array(255,255,0),
'09'=& 50),
'10'=>数组(0,128,128),
'11'=> array(173,216,230),
'12'=> ; array(0,0,255),
'13'=> array(255,105,180),
'14'=& $ b'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];

我对这个解决方案感到非常满意,感谢您的建议。


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.

解决方案

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.

这篇关于PHP将24位颜色转换为4位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆