Skip to content

Commit

Permalink
Merge pull request #2 from xp-framework/feature/color-int
Browse files Browse the repository at this point in the history
Add Color::intValue() to convert colors to ARGB color ints
  • Loading branch information
thekid authored Nov 4, 2023
2 parents 2e92e9a + 7b582ae commit 03ecaef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
49 changes: 29 additions & 20 deletions src/main/php/img/Color.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,49 @@
/**
* Color class
*
* @test xp://net.xp_framework.unittest.img.ColorTest
* @see xp://img.Image
* @test img.unittest.ColorTest
* @see img.Image
*/
class Color {
public
$red = 0,
$green = 0,
$blue = 0;

public
$handle = null;
public $red, $green, $blue;
public $handle= null;

/**
* Constructor
* Creates a new color. Three forms are acceptable:
*
* @param var a string containing the hexadecimal format or
* three ints (red, green blue)
* ```php
* new Color('#990000'); // from hex
* new Color(255, 0, 255); // from rgb components
* new Color(0xffa7ff03); // from ARGB color integer
* ```
*
* @param var... $args
* @see https://developer.android.com/reference/android/graphics/Color#color-ints
*/
public function __construct() {
$a= func_get_args();
if (is_string($a[0])) {
sscanf(ltrim($a[0], '#'), '%2x%2x%2x', $this->red, $this->green, $this->blue);
public function __construct(... $args) {
if (3 === sizeof($args)) {
list($this->red, $this->green, $this->blue)= $args;
} else if (is_int($input= $args[0])) {
$this->red= ($input >> 16) & 0xff;
$this->green= ($input >> 8) & 0xff;
$this->blue= $input & 0xff;
} else {
list($this->red, $this->green, $this->blue)= $a;
sscanf(ltrim($input, '#'), '%2x%2x%2x', $this->red, $this->green, $this->blue);
}
}

/** Compresses this color into an ARGB integer */
public function intValue(): int {
return (255 & 0xff) << 24 | ($this->red & 0xff) << 16 | ($this->green & 0xff) << 8 | ($this->blue & 0xff);
}

/**
* Get RGB value as hexadecimal string (e.g. #990000)
* Get RGB value as hexadecimal string, e.g. `#990000`.
*
* @return string HTML-style color
* @return string
*/
public function toHex() {
return '#'.dechex($this->red).dechex($this->green).dechex($this->blue);
return sprintf('#%02x%02x%02x', $this->red, $this->green, $this->blue);
}

/**
Expand Down
23 changes: 14 additions & 9 deletions src/test/php/img/unittest/ColorTest.class.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
<?php namespace img\unittest;

use img\Color;
use unittest\{Test, Values};
use unittest\{Test, TestCase, Values};

/**
* TestCase
*
* @see xp://img.Color
*/
class ColorTest extends \unittest\TestCase {
class ColorTest extends TestCase {

#[Test, Values(['#a7ff03', 'a7ff03', 'A7FF03', '#A7FF03'])]
public function create_from_hex($value) {
$c= new Color($value);
$this->assertEquals(0xA7, $c->red);
$this->assertEquals(0xFF, $c->green);
$this->assertEquals(0xa7, $c->red);
$this->assertEquals(0xff, $c->green);
$this->assertEquals(0x03, $c->blue);
}

Expand All @@ -35,4 +30,14 @@ public function toHex_returns_lowercase_hex_with_leading_hash() {
public function string_representation() {
$this->assertEquals('img.Color@(239, 010, 007)', (new Color('#ef0a07'))->toString());
}

#[Test, Values([0xff000000, 0xffa7ff03, 0xffffffff])]
public function argb_color_int($argb) {
$this->assertEquals($argb, (new Color($argb))->intValue());
}

#[Test]
public function argb_signed_32_bit() {
$this->assertEquals(0xff464d32, (new Color(-12169934))->intValue());
}
}

0 comments on commit 03ecaef

Please sign in to comment.