BCGColor::g()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace tinymeng\code\Gateways\barcode;
3
4
/**
5
 *--------------------------------------------------------------------
6
 *
7
 * Holds Color in RGB Format.
8
 *
9
 *--------------------------------------------------------------------
10
 * Copyright (C) Jean-Sebastien Goupil
11
 * http://www.barcodephp.com
12
 */
13
class BCGColor {
14
    protected $r, $g, $b;    // int Hexadecimal Value
15
    protected $transparent;
16
17
    /**
18
     * Save RGB value into the classes.
19
     *
20
     * There are 4 way to associate color with this classes :
21
     *  1. Gives 3 parameters int (R, G, B)
22
     *  2. Gives 1 parameter string hex value (#ff0000) (preceding with #)
23
     *  3. Gives 1 parameter int hex value (0xff0000)
24
     *  4. Gives 1 parameter string color code (white, black, orange...)
25
     *
26
     * @param mixed ...
27
     */
28
    public function __construct() {
29
        $args = func_get_args();
30
        $c = count($args);
31
        if ($c === 3) {
32
            $this->r = intval($args[0]);
33
            $this->g = intval($args[1]);
34
            $this->b = intval($args[2]);
35
        } elseif ($c === 1) {
36
            if (is_string($args[0]) && strlen($args[0]) === 7 && $args[0][0] === '#') {        // Hex Value in String
37
                $this->r = intval(substr($args[0], 1, 2), 16);
38
                $this->g = intval(substr($args[0], 3, 2), 16);
39
                $this->b = intval(substr($args[0], 5, 2), 16);
40
            } else {
41
                if (is_string($args[0])) {
42
                    $args[0] = self::getColor($args[0]);
43
                }
44
45
                $args[0] = intval($args[0]);
46
                $this->r = ($args[0] & 0xff0000) >> 16;
47
                $this->g = ($args[0] & 0x00ff00) >> 8;
48
                $this->b = ($args[0] & 0x0000ff);
49
            }
50
        } else {
51
            $this->r = $this->g = $this->b = 0;
52
        }
53
    }
54
55
    /**
56
     * Sets the color transparent.
57
     *
58
     * @param bool $transparent
59
     */
60
    public function setTransparent($transparent) {
61
        $this->transparent = $transparent;
62
    }
63
64
    /**
65
     * Returns Red Color.
66
     *
67
     * @return int
68
     */
69
    public function r() {
70
        return $this->r;
71
    }
72
73
    /**
74
     * Returns Green Color.
75
     *
76
     * @return int
77
     */
78
    public function g() {
79
        return $this->g;
80
    }
81
82
    /**
83
     * Returns Blue Color.
84
     *
85
     * @return int
86
     */
87
    public function b() {
88
        return $this->b;
89
    }
90
91
    /**
92
     * Returns the int value for PHP color.
93
     *
94
     * @param resource $im
95
     * @return int
96
     */
97
    public function allocate(&$im) {
98
        $allocated = imagecolorallocate($im, $this->r, $this->g, $this->b);
99
        if ($this->transparent) {
100
            return imagecolortransparent($im, $allocated);
101
        } else {
102
            return $allocated;
103
        }
104
    }
105
106
    /**
107
     * Returns class of BCGColor depending of the string color.
108
     *
109
     * If the color doens't exist, it takes the default one.
110
     *
111
     * @param string $code
112
     * @param string $default
113
     */
114
    public static function getColor($code, $default = 'white') {
115
        switch(strtolower($code)) {
116
            case '':
117
            case 'white':
118
                return 0xffffff;
119
            case 'black':
120
                return 0x000000;
121
            case 'maroon':
122
                return 0x800000;
123
            case 'red':
124
                return 0xff0000;
125
            case 'orange':
126
                return 0xffa500;
127
            case 'yellow':
128
                return 0xffff00;
129
            case 'olive':
130
                return 0x808000;
131
            case 'purple':
132
                return 0x800080;
133
            case 'fuchsia':
134
                return 0xff00ff;
135
            case 'lime':
136
                return 0x00ff00;
137
            case 'green':
138
                return 0x008000;
139
            case 'navy':
140
                return 0x000080;
141
            case 'blue':
142
                return 0x0000ff;
143
            case 'aqua':
144
                return 0x00ffff;
145
            case 'teal':
146
                return 0x008080;
147
            case 'silver':
148
                return 0xc0c0c0;
149
            case 'gray':
150
                return 0x808080;
151
            default:
152
                return self::getColor($default, 'white');
153
        }
154
    }
155
}
156
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...