Drawing::pointsToCentimeters()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of PHPOffice Common
4
 *
5
 * PHPOffice Common is free software distributed under the terms of the GNU Lesser
6
 * General Public License version 3 as published by the Free Software Foundation.
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code. For the full list of
10
 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
11
 *
12
 * @link        https://github.com/PHPOffice/Common
13
 * @copyright   2009-2016 PHPOffice Common contributors
14
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15
 */
16
17
namespace PhpOffice\Common;
18
19
/**
20
 * \PhpOffice\Common\Drawing
21
 */
22
class Drawing
23
{
24
    const DPI_96 = 96;
25
26
    /**
27
     * Convert pixels to EMU
28
     *
29
     * @param  int $pValue Value in pixels
30
     * @return int
31
     */
32 1
    public static function pixelsToEmu($pValue = 0)
33
    {
34 1
        return round($pValue * 9525);
35
    }
36
37
    /**
38
     * Convert EMU to pixels
39
     *
40
     * @param  int $pValue Value in EMU
41
     * @return int
42
     */
43 1
    public static function emuToPixels($pValue = 0)
44
    {
45 1
        if ($pValue == 0) {
46 1
            return 0;
47
        }
48 1
        return round($pValue / 9525);
49
    }
50
51
    /**
52
     * Convert pixels to points
53
     *
54
     * @param  int $pValue Value in pixels
55
     * @return float
56
     */
57 1
    public static function pixelsToPoints($pValue = 0)
58
    {
59 1
        return $pValue * 0.67777777;
60
    }
61
62
    /**
63
     * Convert points width to centimeters
64
     *
65
     * @param  int $pValue Value in points
66
     * @return float
67
     */
68 1
    public static function pointsToCentimeters($pValue = 0)
69
    {
70 1
        if ($pValue == 0) {
71 1
            return 0;
72
        }
73 1
        return ((($pValue * 1.333333333) / self::DPI_96) * 2.54);
74
    }
75
    
76
    /**
77
     * Convert points width to pixels
78
     *
79
     * @param  int $pValue Value in points
80
     * @return float
81
     */
82 1
    public static function pointsToPixels($pValue = 0)
83
    {
84 1
        if ($pValue == 0) {
85 1
            return 0;
86
        }
87 1
        return $pValue * 1.333333333;
88
    }
89
90
    /**
91
     * Convert pixels to centimeters
92
     *
93
     * @param  int $pValue Value in pixels
94
     * @return float
95
     */
96 1
    public static function pixelsToCentimeters($pValue = 0)
97
    {
98
        //return $pValue * 0.028;
99 1
        return (($pValue / self::DPI_96) * 2.54);
100
    }
101
102
    /**
103
     * Convert centimeters width to pixels
104
     *
105
     * @param  int $pValue Value in centimeters
106
     * @return float
107
     */
108 1
    public static function centimetersToPixels($pValue = 0)
109
    {
110 1
        if ($pValue == 0) {
111 1
            return 0;
112
        }
113 1
        return ($pValue / 2.54) * self::DPI_96;
114
    }
115
116
    /**
117
     * Convert degrees to angle
118
     *
119
     * @param  int $pValue Degrees
120
     * @return int
121
     */
122 1
    public static function degreesToAngle($pValue = 0)
123
    {
124 1
        return (int) round($pValue * 60000);
125
    }
126
127
    /**
128
     * Convert angle to degrees
129
     *
130
     * @param  int $pValue Angle
131
     * @return int
132
     */
133 1
    public static function angleToDegrees($pValue = 0)
134
    {
135 1
        if ($pValue == 0) {
136 1
            return 0;
137
        }
138 1
        return round($pValue / 60000);
139
    }
140
141
    /**
142
     * Convert centimeters width to twips
143
     *
144
     * @param integer $pValue
145
     * @return float
146
     */
147 1
    public static function centimetersToTwips($pValue = 0)
148
    {
149 1
        if ($pValue == 0) {
150 1
            return 0;
151
        }
152 1
        return $pValue * 566.928;
153
    }
154
155
    /**
156
     * Convert twips width to centimeters
157
     *
158
     * @param integer $pValue
159
     * @return float
160
     */
161 1
    public static function twipsToCentimeters($pValue = 0)
162
    {
163 1
        if ($pValue == 0) {
164 1
            return 0;
165
        }
166 1
        return $pValue / 566.928;
167
    }
168
169
    /**
170
     * Convert inches width to twips
171
     *
172
     * @param integer $pValue
173
     * @return float
174
     */
175 1
    public static function inchesToTwips($pValue = 0)
176
    {
177 1
        if ($pValue == 0) {
178 1
            return 0;
179
        }
180 1
        return $pValue * 1440;
181
    }
182
183
    /**
184
     * Convert twips width to inches
185
     *
186
     * @param integer $pValue
187
     * @return float
188
     */
189 1
    public static function twipsToInches($pValue = 0)
190
    {
191 1
        if ($pValue == 0) {
192 1
            return 0;
193
        }
194 1
        return $pValue / 1440;
195
    }
196
197
    /**
198
     * Convert twips width to pixels
199
     *
200
     * @param integer $pValue
201
     * @return float
202
     */
203 1
    public static function twipsToPixels($pValue = 0)
204
    {
205 1
        if ($pValue == 0) {
206 1
            return 0;
207
        }
208 1
        return round($pValue / 15.873984);
209
    }
210
211
    /**
212
     * Convert HTML hexadecimal to RGB
213
     *
214
     * @param string $pValue HTML Color in hexadecimal
215
     * @return array|false Value in RGB
216
     */
217 1
    public static function htmlToRGB($pValue)
218
    {
219 1
        if ($pValue[0] == '#') {
220 1
            $pValue = substr($pValue, 1);
221
        }
222
223 1
        if (strlen($pValue) == 6) {
224 1
            list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[1], $pValue[2] . $pValue[3], $pValue[4] . $pValue[5]);
225 1
        } elseif (strlen($pValue) == 3) {
226 1
            list($colorR, $colorG, $colorB) = array($pValue[0] . $pValue[0], $pValue[1] . $pValue[1], $pValue[2] . $pValue[2]);
227
        } else {
228 1
            return false;
229
        }
230
231 1
        $colorR = hexdec($colorR);
232 1
        $colorG = hexdec($colorG);
233 1
        $colorB = hexdec($colorB);
234
235 1
        return array($colorR, $colorG, $colorB);
236
    }
237
}
238