Skip to content

Commit

Permalink
Added PHP CodeSniffer to project (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaykun committed Aug 6, 2020
1 parent ffdf20b commit c75f7e4
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 108 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
@@ -0,0 +1,17 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

# .travis.yml
[.travis.yml]
indent_size = 2
10 changes: 10 additions & 0 deletions .phpcs.xml
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHPColors"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<description>The coding standard for PHPColors.</description>

<file>./src</file>
<file>./tests</file>

<rule ref="PSR12"/>
</ruleset>
7 changes: 3 additions & 4 deletions .travis.yml
Expand Up @@ -3,9 +3,8 @@ php:
- 7.2
- 7.3
- 7.4
script:
- vendor/bin/tester tests -s -p php

before_script:
# Install Nette Tester
- composer install --no-interaction --dev --prefer-source
script:
- vendor/bin/phpcs --extensions=php,phpt --warning-severity=0
- vendor/bin/tester tests -s -p php
25 changes: 17 additions & 8 deletions README.md
@@ -1,20 +1,28 @@
### Last Build: [![Build Status](https://secure.travis-ci.org/mexitek/phpColors.png)](http://travis-ci.org/mexitek/phpColors)
# PHPColors [![Build Status](https://travis-ci.org/mexitek/phpColors.svg?branch=master)](https://travis-ci.org/mexitek/phpColors)

> A series of methods that let you manipulate colors. Just incase you ever need different shades of one color on the fly.
## Requirements

PHPColors requires PHP version 7.2.0 or greater.

## Installation

### Composer

Simply add `mexitek/phpcolors` to `composer.json` using `dev-master`.

`composer require mexitek/phpcolors dev-master`
```
composer require mexitek/phpcolors:dev-master
```

## How it works
Instantiate an object of the color class with a hex color string `$foo = new Color("336699")`. That's it! Now, call the methods you need for different color variants.

## Available Methods
- <strong>darken( [$amount] )</strong> : Allows you to obtain a darker shade of your color. Optionally you can decide to darken using a desired percentage.
- <strong>lighten( [$amount] )</strong> : Allows you to obtain a lighter shade of your color. Optionally you can decide to lighten using a desired percentage.
- <strong>mix($hex, [$amount] )</strong> : Allows you to mix another color to your color. Optionally you can decide to set the percent of second color or original color amount is ranged -100..0.100.
- <strong>mix($hex, [$amount] )</strong> : Allows you to mix another color to your color. Optionally you can decide to set the percent of second color or original color amount is ranged -100...0...100.
- <strong>isLight( [$hex] )</strong> : Determins whether your color (or the provide param) is considered a "light" color. Returns `TRUE` if color is light.
- <strong>isDark( [$hex] )</strong> : Determins whether your color (or the provide param) is considered a "dark" color. Returns `TRUE` if color is dark.
- <strong>makeGradient( [$amount] )</strong> : Returns an array with 2 indices `light` and `dark`, the initial color will either be selected for `light` or `dark` depending on its brightness, then the other color will be generated. The optional param allows for a static lighten or darkened amount.
Expand Down Expand Up @@ -110,8 +118,8 @@ echo $myBlue->getCssGradient();
background: #336699;

// IE Browsers
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8cb3d9', endColorstr='#336699');
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8cb3d9', endColorstr='#336699');

// Safari 5.1+, Mobile Safari, Chrome 10+
background-image: -webkit-linear-gradient(top, #8cb3d9, #336699);

Expand All @@ -132,7 +140,7 @@ $myBlue = new Color("#336699");
// Get CSS
echo $myBlue->getCssGradient(10, TRUE);
/* - Actual output doesn't have comments and is single line

background: #336699; // fallback background
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8cb3d9', endColorstr='#336699'); // IE Browsers
background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#8cb3d9), to(#336699)); // Safari 4+, Chrome 1-9
Expand All @@ -149,7 +157,7 @@ echo $myBlue->getCssGradient(10, TRUE);
- mexitek
- danielpataki
- alexmglover
- intuxicated
- intuxicated
- pborreli
- curtisgibby
- matthewpatterson
Expand All @@ -160,4 +168,5 @@ echo $myBlue->getCssGradient(10, TRUE);
- thedavidmeister
- tylercd100

# License: [arlo.mit-license.org](http://arlo.mit-license.org)
# License
See LICENSE file or [arlo.mit-license.org](http://arlo.mit-license.org)
30 changes: 24 additions & 6 deletions composer.json
@@ -1,19 +1,37 @@
{
"name": "mexitek/phpcolors",
"description": "A series of methods that let you manipulate colors. Just incase you ever need different shades of one color on the fly.",
"keywords": ["color", "ui", "css", "frontend", "design"],
"type": "library",
"keywords": [
"color",
"ui",
"css",
"frontend",
"design"
],
"homepage": "http://mexitek.github.com/phpColors/",
"license": "MIT",
"authors": [
{
"name": "Arlo Carreon",
"homepage": "http://arlocarreon.com",
"role": "creator"
}
],
"support": {
"issues": "https://github.com/mexitek/phpColors/issues",
"source": "https://github.com/mexitek/phpColors"
},
"require": {
"php": "^7.2"
},
"require-dev": {
"nette/tester": "^2.3"
"nette/tester": "^2.3",
"squizlabs/php_codesniffer": "^3.5"
},
"authors" : [
{ "name": "Arlo Carreon", "homepage":"http://arlocarreon.com", "role" : "creator" }
],
"autoload": {
"classmap": ["src"]
"classmap": [
"src"
]
}
}
32 changes: 16 additions & 16 deletions tests/colorAnalyze.phpt
Expand Up @@ -7,29 +7,29 @@ use Tester\Assert;


$isDark = array(
"000000" => TRUE,
"336699" => TRUE,
"913399" => TRUE,
"E5C3E8" => FALSE,
"D7E8DD" => FALSE,
"218A47" => TRUE,
"3D41CA" => TRUE,
"E5CCDD" => FALSE,
"FFFFFF" => FALSE,
"000000" => true,
"336699" => true,
"913399" => true,
"E5C3E8" => false,
"D7E8DD" => false,
"218A47" => true,
"3D41CA" => true,
"E5CCDD" => false,
"FFFFFF" => false,
);

foreach ($isDark as $colorHex => $state) {
$color = new Color($colorHex);
Assert::same($state, $color->isDark(), 'Incorrect dark color analyzed (#'. $colorHex .').');
$color = new Color($colorHex);
Assert::same($state, $color->isDark(), 'Incorrect dark color analyzed (#' . $colorHex . ').');
}

$isLight = array(
"FFFFFF" => TRUE,
"A3FFE5" => TRUE,
"000000" => FALSE,
"FFFFFF" => true,
"A3FFE5" => true,
"000000" => false,
);

foreach ($isLight as $colorHex => $state) {
$color = new Color($colorHex);
Assert::same($state, $color->isLight(), 'Incorrect light color analyzed (#'. $colorHex .').');
$color = new Color($colorHex);
Assert::same($state, $color->isLight(), 'Incorrect light color analyzed (#' . $colorHex . ').');
}
16 changes: 8 additions & 8 deletions tests/colorChange.phpt
Expand Up @@ -7,22 +7,22 @@ use Tester\Assert;


$expected = array(
"336699" => "264d73",
"913399" => "6d2673"
"336699" => "264d73",
"913399" => "6d2673"
);

foreach ($expected as $original => $darker) {
$color = new Color($original);
Assert::same($darker, $color->darken(), 'Incorrect darker color returned.');
$color = new Color($original);
Assert::same($darker, $color->darken(), 'Incorrect darker color returned.');
}


$expected = array(
"336699" => "4080bf",
"913399" => "b540bf"
"336699" => "4080bf",
"913399" => "b540bf"
);

foreach ($expected as $original => $lighter) {
$color = new Color($original);
Assert::same($lighter, $color->lighten(), "Incorrect lighter color returned.");
$color = new Color($original);
Assert::same($lighter, $color->lighten(), "Incorrect lighter color returned.");
}
32 changes: 15 additions & 17 deletions tests/colorComplementary.phpt
Expand Up @@ -7,24 +7,22 @@ use Tester\Assert;


$expected = array(
"ff0000" => "00ffff",
"0000ff" => "ffff00",
"00ff00" => "ff00ff",
"ffff00" => "0000ff",
"00ffff" => "ff0000",
"ffff00" => "0000ff",

"49cbaf" => "cb4965",
"003eb2" => "b27400",
"b27400" => "003eb2",
"ffff99" => "9999ff",
"ccff00" => "3300ff",
"3300ff" => "ccff00",
"fb4a2c" => "2cddfb",
"9cebff" => "ffb09c",
"ff0000" => "00ffff",
"0000ff" => "ffff00",
"00ff00" => "ff00ff",
"ffff00" => "0000ff",
"00ffff" => "ff0000",
"49cbaf" => "cb4965",
"003eb2" => "b27400",
"b27400" => "003eb2",
"ffff99" => "9999ff",
"ccff00" => "3300ff",
"3300ff" => "ccff00",
"fb4a2c" => "2cddfb",
"9cebff" => "ffb09c",
);

foreach ($expected as $original => $complementary) {
$color = new Color($original);
Assert::same($complementary, $color->complementary(), 'Incorrect complementary color returned.');
$color = new Color($original);
Assert::same($complementary, $color->complementary(), 'Incorrect complementary color returned.');
}
76 changes: 38 additions & 38 deletions tests/colorConvert.phpt
Expand Up @@ -7,57 +7,57 @@ use Tester\Assert;

// Colors in RGB, for testing.
$blue = [
'R' => 0,
'G' => 158,
'B' => 204,
'R' => 0,
'G' => 158,
'B' => 204,
];
$yellow = [
'R' => 244,
'G' => 231,
'B' => 15,
'R' => 244,
'G' => 231,
'B' => 15,
];
$black = [
'R' => 0,
'G' => 0,
'B' => 0,
'R' => 0,
'G' => 0,
'B' => 0,
];
$white = [
'R' => 255,
'G' => 255,
'B' => 255,
'R' => 255,
'G' => 255,
'B' => 255,
];

// Test cases.
$colorsToConvert = array(
'blue' => [ // rgb(0, 158, 204)
'hex' => '009ecc',
'rgb' => $blue,
],
'yellow' => [ // rgb(244, 231, 15)
'hex' => 'f4e70f',
'rgb' => $yellow,
],
'black' => [
'hex' => '000000',
'rgb' => $black,
],
'white' => [
'hex' => 'ffffff',
'rgb' => $white,
],
'blue' => [ // rgb(0, 158, 204)
'hex' => '009ecc',
'rgb' => $blue,
],
'yellow' => [ // rgb(244, 231, 15)
'hex' => 'f4e70f',
'rgb' => $yellow,
],
'black' => [
'hex' => '000000',
'rgb' => $black,
],
'white' => [
'hex' => 'ffffff',
'rgb' => $white,
],
);


foreach ($colorsToConvert as $color) {
$rgb = $color['rgb'];
$hex = $color['hex'];
$rgb = $color['rgb'];
$hex = $color['hex'];

$answer = Color::rgbToHex($rgb);
Assert::same(
$hex,
$answer,
'Incorrect hex result: "' . Color::rgbToString($rgb) .
'" should convert to "' . $hex .
'", but output was: "' . $answer . '".'
);
$answer = Color::rgbToHex($rgb);
Assert::same(
$hex,
$answer,
'Incorrect hex result: "' . Color::rgbToString($rgb) .
'" should convert to "' . $hex .
'", but output was: "' . $answer . '".'
);
}
22 changes: 11 additions & 11 deletions tests/colorMix.phpt
Expand Up @@ -7,18 +7,18 @@ use Tester\Assert;


$expected = array(
"ffffff" => array("ff0000","ff7f7f"), // ffffff + ff0000 = ff7f7f
"00ff00" => array("ff0000","7f7f00"),
"000000" => array("ff0000","7f0000"),
"002fff" => array("000000","00177f"),
"00ffed" => array("000000","007f76"),
"ff9a00" => array("000000","7f4d00"),
"ff9a00" => array("ffffff","ffcc7f"),
"00ff2d" => array("ffffff","7fff96"),
"8D43B4" => array("35CF64","61898c"),
"ffffff" => array("ff0000", "ff7f7f"), // ffffff + ff0000 = ff7f7f
"00ff00" => array("ff0000", "7f7f00"),
"000000" => array("ff0000", "7f0000"),
"002fff" => array("000000", "00177f"),
"00ffed" => array("000000", "007f76"),
"ff9a00" => array("000000", "7f4d00"),
"ff9a00" => array("ffffff", "ffcc7f"),
"00ff2d" => array("ffffff", "7fff96"),
"8D43B4" => array("35CF64", "61898c"),
);

foreach ($expected as $original => $complementary) {
$color = new Color($original);
Assert::same($complementary[1], $color->mix($complementary[0]), 'Incorrect mix color returned.');
$color = new Color($original);
Assert::same($complementary[1], $color->mix($complementary[0]), 'Incorrect mix color returned.');
}

0 comments on commit c75f7e4

Please sign in to comment.