From b8c85813b09bbb76b88940a55df9085d2afde0eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 5 Dec 2011 16:53:12 +0100 Subject: [PATCH] Moving gradient into ChatUtil --- .../data/chat/message/ChatMessage.class.php | 13 ++----- file/lib/util/ChatUtil.class.php | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 file/lib/util/ChatUtil.class.php diff --git a/file/lib/data/chat/message/ChatMessage.class.php b/file/lib/data/chat/message/ChatMessage.class.php index 3e8f2a1..582556f 100644 --- a/file/lib/data/chat/message/ChatMessage.class.php +++ b/file/lib/data/chat/message/ChatMessage.class.php @@ -66,18 +66,11 @@ public function getFormattedMessage() { * @return string */ public function getFormattedUsername() { - if ($this->type == self::TYPE_INFORMATION) return ''.$this->getUsername().''; + $username = $this->getUsername(); - $string = str_split($this->getUsername()); - $r = (int) (($this->color1 >> 16 & 255) - ($this->color2 >> 16 & 255)) / (count($string) - 1); - $g = (int) (($this->color1 >> 8 & 255) - ($this->color2 >> 8 & 255)) / (count($string) - 1); - $b = (int) (($this->color1 & 255) - ($this->color2 & 255)) / (count($string) - 1); - $result = ''; - for ($i = 0, $max = count($string); $i < $max; $i++) { - $result .= ''.$string[$i].''; - } + if ($this->type != self::TYPE_INFORMATION) $username = \wcf\util\ChatUtil::gradient($username, $this->color1, $this->color2); - return ''.$result.''; + return ''.$username.''; } /** diff --git a/file/lib/util/ChatUtil.class.php b/file/lib/util/ChatUtil.class.php new file mode 100644 index 0000000..9b0b42d --- /dev/null +++ b/file/lib/util/ChatUtil.class.php @@ -0,0 +1,37 @@ + + * @package timwolla.wcf.chat + * @subpackage util + */ +class ChatUtil { + /** + * Creates a gradient out of two colors represented by an integer. + * The first byte is red, the second byte is green, the third one is blue. + * The numbers can be easily expressed in hexadecimal notation: 0xFF0000 being red. + * + * @param string $string + * @param integer $start + * @param integer $end + * @returen string + */ + public static function gradient($string, $start, $end) { + $string = str_split($string); + $r = (int) ((($start >> 16 & 255) - ($end >> 16 & 255)) / (count($string) - 1)); + $g = (int) ((($start >> 8 & 255) - ($end >> 8 & 255)) / (count($string) - 1)); + $b = (int) ((($start & 255) - ($end & 255)) / (count($string) - 1)); + + $result = ''; + for ($i = 0, $max = count($string); $i < $max; $i++) { + $result .= ''.$string[$i].''; + } + + return $result; + } +}