/** * Restores the state of precision setting * */ public static function restore() { if (self::$oldPrecision !== null && function_exists('ini_set')) { ini_set('precision', self::$oldPrecision[0]); ini_set('bcmath.scale', self::$oldPrecision[1]); bcscale(self::$oldPrecision[1]); } self::$setup = false; }
public function testIssue() { if ($this->locale === false) { $this->markTestSkipped('The test locale could not be set.'); } ILess_Math::setup(); ILess_UnitConversion::setup(); $this->assertEquals(array('rad' => '0.1591549430918953', 'deg' => '0.0027777777777777', 'grad' => '0.0025000000000000', 'turn' => '1'), ILess_UnitConversion::$angle, 'The math setup works'); }
/** * Setups conversions with given precision. * */ public static function setup() { if (self::$setup !== false) { return; } self::$angle = array('rad' => ILess_Math::divide(1, ILess_Math::multiply('2', str_replace(',', '.', M_PI))), 'deg' => ILess_Math::divide(1, 360), 'grad' => ILess_Math::divide(1, 400), 'turn' => '1'); self::$length = array('m' => '1', 'cm' => '0.01', 'mm' => '0.001', 'in' => '0.0254', 'pt' => ILess_Math::divide('0.0254', '72'), 'pc' => ILess_Math::multiply(ILess_Math::divide('0.0254', '72'), '12')); self::$setup = true; }
public function setUp() { $this->registry = new ILess_FunctionRegistry(array(), new ILess_Environment()); ILess_Math::setup(); }
/** * Restores the math precision * * @return void */ private function restoreMathAndLocale() { ILess_Math::restore(); ILess_UnitConversion::restore(); }
/** * Returns the 'value' channel of @color in the HSV space * * @param ILess_Node_Color $color * @return string */ public function hsvvalue(ILess_Node $color) { if (!$color instanceof ILess_Node_Color) { return $color; } $hsv = $color->toHSV(); return new ILess_Node_Dimension(ILess_Math::round($hsv['v'] * 100), '%'); }
/** * Operations have to be done per-channel, if not, * channels will spill onto each other. Once we have * our result, in the form of an integer triplet, * we create a new color node to hold the result. * * @param ILess_Environment $env * @param string $op * @param ILess_Node $other * @return ILess_Node_Color * @throws InvalidArgumentException */ public function operate(ILess_Environment $env, $op, ILess_Node $other) { $result = array(); if (!$other instanceof ILess_Node_Color) { if (!self::methodExists($other, 'toColor')) { throw new InvalidArgumentException('The other node must implement toColor() method to operate'); } $other = $other->toColor(); if (!$other instanceof ILess_Node_Color) { throw new InvalidArgumentException('The toColor() method must return an instance of ILess_Node_Color'); } } $t = $this->getRGB(); $o = $other->getRGB(); for ($c = 0; $c < 3; $c++) { $result[$c] = ILess_Math::operate($op, $t[$c], $o[$c]); if ($result[$c] > 255) { $result[$c] = 255; } elseif ($result < 0) { $result[$c] = 0; } } return new ILess_Node_Color($result, $this->color->getAlpha() + $other->color->getAlpha()); }
/** * Returns the string representation in ARGB model * * @return string */ public function toARGB() { $argb = array_merge(array(ILess_Math::clean(ILess_Math::round($this->alpha * 256))), $this->rgb); $result = ''; foreach ($argb as $i) { $i = ILess_Math::round($i); $i = dechex($i > 255 ? 255 : ($i < 0 ? 0 : $i)); $result .= str_pad($i, 2, '0', STR_PAD_LEFT); } return '#' . $result; }
public function setUp() { // we need the precision setup ILess_Math::setup(16); }
/** * @covers round * @dataProvider getDataForRoundTest */ public function testRound($value, $precision, $expected) { $this->assertEquals($expected, ILess_Math::round($value, $precision), sprintf('Rounding of "%s" with precision "%s" works', $value, $precision)); }
public function __construct(ILess_Environment $env, ILess_Importer $importer) { parent::__construct($env, $importer); ILess_Math::setup(16); ILess_UnitConversion::setup(); }
/** * Converts to another unit * * @param array|string $conversions * @return ILess_Node_Dimension */ public function convertTo($conversions) { $value = $this->value; $unit = clone $this->unit; if (is_string($conversions)) { $derivedConversions = array(); foreach (ILess_UnitConversion::$groups as $i) { if (isset(ILess_UnitConversion::${$i}[$conversions])) { $derivedConversions = array($i => $conversions); } } $conversions = $derivedConversions; } foreach ($conversions as $groupName => $targetUnit) { $group = ILess_UnitConversion::${$groupName}; // numerator for ($i = 0, $count = count($unit->numerator); $i < $count; $i++) { $atomicUnit = $unit->numerator[$i]; if (is_object($atomicUnit)) { continue; } if (!isset($group[$atomicUnit])) { continue; } $value = ILess_Math::multiply($value, ILess_Math::divide($group[$atomicUnit], $group[$targetUnit])); $unit->numerator[$i] = $targetUnit; } // denominator for ($i = 0, $count = count($unit->denominator); $i < $count; $i++) { $atomicUnit = $unit->denominator[$i]; if (!isset($group[$atomicUnit])) { continue; } // $value = $value / ($group[$atomicUnit] / $group[$targetUnit]); $value = ILess_Math::divide($value, ILess_Math::divide($group[$atomicUnit], $group[$targetUnit])); $unit->denominator[$i] = $targetUnit; } } $unit->cancel(); return new ILess_Node_Dimension($value, $unit); }