/** * @inheritdoc */ public function generateCSS(Context $context, OutputInterface $output) { if ($context->strictUnits && !$this->unit->isSingular()) { throw new CompilerException(sprintf('Multiple units in dimension. Correct the units or use the unit function. Bad unit: %s', $this->unit->toString())); } $value = Util::round($context, $this->value); $strValue = (string) $value; if ($value !== 0 && $value < 1.0E-6 && $value > -1.0E-6) { $strValue = Math::toFixed($value, 20); } // remove trailing zeros $strValue = Math::clean($strValue); // Zero values doesn't need a unit if ($context->compress) { if ($value == 0 && $this->unit->isLength()) { $output->add($strValue); return; } // Float values doesn't need a leading zero if ($value > 0 && $value < 1) { $strValue = substr($strValue, 1); } } $output->add($strValue); // pass to unit $this->unit->generateCSS($context, $output); }
/** * @covers toFixed * @dataProvider getDataForToFixedTest */ public function testToFixed($test, $decimals, $expected) { $this->assertEquals($expected, Math::toFixed($test, $decimals)); }
/** * Round the value using the `$context->precision` setting. * * @param Context $context * @param mixed $value * * @return string */ public static function round(Context $context, $value) { // add "epsilon" to ensure numbers like 1.000000005 (represented as 1.000000004999....) are properly rounded... return $context->numPrecision === null ? $value : Math::toFixed($value + 2.0E-16, $context->numPrecision); }
/** * Returns the color as HEX string (when transparency present, in RGBA model) * * @param boolean $compress Compress the color? * @param boolean $canShorten Can the color be shortened if possible? * @return string */ public function toString($compress = false, $canShorten = false) { if ($this->isTransparentKeyword) { return 'transparent'; } if ($this->originalForm) { return $this->originalForm; } $alpha = Math::toFixed($this->alpha + 2.0E-16, 8); if ($alpha < 1) { $fixedRGB = $this->getFixedRGB(); return sprintf('rgba(%s)', join($compress ? ',' : ', ', [$fixedRGB[0], $fixedRGB[1], $fixedRGB[2], Math::clean($this->clamp($alpha, 1))])); } // prevent named colors if ($this->keyword) { return $this->keyword; } $color = []; foreach ($this->getFixedRgb() as $i) { $color[] = str_pad(dechex(Math::round($i)), 2, '0', STR_PAD_LEFT); } $color = join('', $color); // convert color to short format if ($canShorten && $color[0] === $color[1] && $color[2] === $color[3] && $color[4] === $color[5]) { $color = $color[0] . $color[2] . $color[4]; } $color = '#' . $color; return $color; }