Skip to content

Commit

Permalink
refactor HsbTest
Browse files Browse the repository at this point in the history
  • Loading branch information
AyoobMH committed Nov 13, 2022
1 parent 64cab66 commit e771f65
Showing 1 changed file with 123 additions and 172 deletions.
295 changes: 123 additions & 172 deletions tests/HsbTest.php
Original file line number Diff line number Diff line change
@@ -1,176 +1,127 @@
<?php

namespace Spatie\Color\Test;

use PHPUnit\Framework\TestCase;
use Spatie\Color\Exceptions\InvalidColorValue;
use Spatie\Color\Hsb;

class HsbTest extends TestCase
{
/** @test */
public function it_is_initializable()
{
$hsb = new Hsb(55, 55, 67);

$this->assertInstanceOf(Hsb::class, $hsb);
$this->assertSame(55.0, $hsb->hue());
$this->assertSame(55.0, $hsb->saturation());
$this->assertSame(67.0, $hsb->brightness());
}

/** @test */
public function it_cant_be_initialized_with_a_negative_saturation()
{
$this->expectException(InvalidColorValue::class);

new Hsb(-5, -1, 67);
}

/** @test */
public function it_cant_be_initialized_with_a_saturation_higher_than_100()
{
$this->expectException(InvalidColorValue::class);

new Hsb(-5, 105, 67);
}

/** @test */
public function it_cant_be_initialized_with_a_negative_brightness()
{
$this->expectException(InvalidColorValue::class);

new Hsb(-5, 55, -67);
}

/** @test */
public function it_cant_be_initialized_with_a_brightness_higher_than_100()
{
$this->expectException(InvalidColorValue::class);

new Hsb(-5, 55, 107);
}

/** @test */
public function it_can_be_created_from_a_string()
{
$hsb = Hsb::fromString('hsb(205,35%,17%)');

$this->assertInstanceOf(Hsb::class, $hsb);
$this->assertSame(205.0, $hsb->hue());
$this->assertSame(35.0, $hsb->saturation());
$this->assertSame(17.0, $hsb->brightness());
}

/** @test */
public function it_can_be_created_from_a_string_without_percentages()
{
$hsb = Hsb::fromString('hsb(205,35,17)');

$this->assertInstanceOf(Hsb::class, $hsb);
$this->assertSame(205.0, $hsb->hue());
$this->assertSame(35.0, $hsb->saturation());
$this->assertSame(17.0, $hsb->brightness());
}

/** @test */
public function it_can_be_created_from_a_string_with_spaces()
{
$hsb = Hsb::fromString(' hsb( 205 , 35% , 17% ) ');

$this->assertInstanceOf(Hsb::class, $hsb);
$this->assertSame(205.0, $hsb->hue());
$this->assertSame(35.0, $hsb->saturation());
$this->assertSame(17.0, $hsb->brightness());
}

/** @test */
public function it_cant_be_created_from_malformed_string()
{
$this->expectException(InvalidColorValue::class);

Hsb::fromString('hsb(55,155,255');
}

/** @test */
public function it_cant_be_created_from_a_string_with_text_around()
{
$this->expectException(InvalidColorValue::class);

Hsb::fromString('abc hsb(55,155,255) abc');
}

/** @test */
public function it_can_be_casted_to_a_string()
{
$hsb = new Hsb(55, 15, 25);

$this->assertSame('hsb(55,15%,25%)', (string) $hsb);
}

/** @test */
public function it_can_be_converted_to_CIELab()
{
$hsb = new Hsb(50, 50, 50);
$lab = $hsb->toCIELab();

$this->assertSame(49.11, $lab->l());
$this->assertSame(-3.48, $lab->a());
$this->assertSame(30.6, $lab->b());
}

/** @test */
public function it_can_be_converted_to_cmyk()
{
$hsb = new Hsb(55, 55, 67);
$cmyk = $hsb->toCmyk();

$this->assertSame($hsb->red(), $cmyk->red());
$this->assertSame($hsb->green(), $cmyk->green());
$this->assertSame($hsb->blue(), $cmyk->blue());
}

/** @test */
public function it_can_be_converted_to_rgb()
{
$hsb = new Hsb(50, 50, 50);
$rgb = $hsb->toRgb();

$this->assertSame(128, $rgb->red());
$this->assertSame(117, $rgb->green());
$this->assertSame(64, $rgb->blue());
}

/** @test */
public function it_can_be_converted_to_rgba_with_a_specific_alpha_value()
{
$hsb = new Hsb(50, 50, 50);
$rgba = $hsb->toRgba(0.5);

$this->assertSame(128, $rgba->red());
$this->assertSame(117, $rgba->green());
$this->assertSame(64, $rgba->blue());
$this->assertSame(0.5, $rgba->alpha());
}

/** @test */
public function it_can_be_converted_to_hex()
{
$hsb = new Hsb(50, 50, 50);
$hex = $hsb->toHex();

$this->assertSame('80', $hex->red());
$this->assertSame('75', $hex->green());
$this->assertSame('40', $hex->blue());
}

public function it_can_be_converted_to_xyz()
{
$hsb = new Hsb(55, 55, 67);
$xyz = $hsb->toXyz();

$this->assertSame(55.1174, $xyz->x());
$this->assertSame(61.8333, $xyz->y());
$this->assertSame(28.4321, $xyz->z());
}
}
use function PHPUnit\Framework\assertInstanceOf;
use function PHPUnit\Framework\assertSame;

it('is initializable', function () {
$hsb = new Hsb(55, 55, 67);

assertInstanceOf(Hsb::class, $hsb);
assertSame(55.0, $hsb->hue());
assertSame(55.0, $hsb->saturation());
assertSame(67.0, $hsb->brightness());
});

it('cant be initialized with a negative saturation', function () {
new Hsb(-5, -1, 67);
})->throws(InvalidColorValue::class);

it('cant be initialized with a saturation higher than 100', function () {
new Hsb(-5, 105, 67);
})->throws(InvalidColorValue::class);

it('cant be initialized with a negative brightness', function () {
new Hsb(-5, 55, -67);
})->throws(InvalidColorValue::class);

it('cant be initialized with a brightness higher than 100', function () {
new Hsb(-5, 55, 107);
})->throws(InvalidColorValue::class);

it('can be created from a string', function () {
$hsb = Hsb::fromString('hsb(205,35%,17%)');

assertInstanceOf(Hsb::class, $hsb);
assertSame(205.0, $hsb->hue());
assertSame(35.0, $hsb->saturation());
assertSame(17.0, $hsb->brightness());
});

it('can be created from a string without percentages', function () {
$hsb = Hsb::fromString('hsb(205,35,17)');

assertInstanceOf(Hsb::class, $hsb);
assertSame(205.0, $hsb->hue());
assertSame(35.0, $hsb->saturation());
assertSame(17.0, $hsb->brightness());
});

it('can be created from a string with spaces', function () {
$hsb = Hsb::fromString(' hsb( 205 , 35% , 17% ) ');

assertInstanceOf(Hsb::class, $hsb);
assertSame(205.0, $hsb->hue());
assertSame(35.0, $hsb->saturation());
assertSame(17.0, $hsb->brightness());
});

it('cant be created from malformed string', function () {
Hsb::fromString('hsb(55,155,255');
})->throws(InvalidColorValue::class);

it('cant be created from a string with text around', function () {
Hsb::fromString('abc hsb(55,155,255) abc');
})->throws(InvalidColorValue::class);

it('can be casted to a string', function () {
$hsb = new Hsb(55, 15, 25);

assertSame('hsb(55,15%,25%)', (string) $hsb);
});

it('can be converted to CIELab', function () {
$hsb = new Hsb(50, 50, 50);
$lab = $hsb->toCIELab();

assertSame(49.11, $lab->l());
assertSame(-3.48, $lab->a());
assertSame(30.6, $lab->b());
});

it('can be converted to cmyk', function () {
$hsb = new Hsb(55, 55, 67);
$cmyk = $hsb->toCmyk();

assertSame($hsb->red(), $cmyk->red());
assertSame($hsb->green(), $cmyk->green());
assertSame($hsb->blue(), $cmyk->blue());
});

it('can be converted to rgb', function () {
$hsb = new Hsb(50, 50, 50);
$rgb = $hsb->toRgb();

assertSame(128, $rgb->red());
assertSame(117, $rgb->green());
assertSame(64, $rgb->blue());
});

it('can be converted to rgba with a specific alpha value', function () {
$hsb = new Hsb(50, 50, 50);
$rgba = $hsb->toRgba(0.5);

assertSame(128, $rgba->red());
assertSame(117, $rgba->green());
assertSame(64, $rgba->blue());
assertSame(0.5, $rgba->alpha());
});

it('can be converted to hex', function () {
$hsb = new Hsb(50, 50, 50);
$hex = $hsb->toHex();

assertSame('80', $hex->red());
assertSame('75', $hex->green());
assertSame('40', $hex->blue());
});

it('can be converted to xyz', function () {
$hsb = new Hsb(55, 55, 67);
$xyz = $hsb->toXyz();

assertSame(55.1174, $xyz->x());
assertSame(61.8333, $xyz->y());
assertSame(28.4321, $xyz->z());
})->skip();

0 comments on commit e771f65

Please sign in to comment.