/** * 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; }
/** * Scales the number to percentage * * @param ILess_Node_Dimension $n The number * @param integer $size * @return float */ public function scaled($n, $size = 255) { if ($n instanceof ILess_Node_Dimension && $n->unit->is('%')) { return ILess_Math::multiply($n->value, ILess_Math::divide($size, '100')); } else { return $this->number($n); } }
/** * Returns the luma * * @param boolean $raw Return raw value? * @return mixed ILess_Node_Dimension if $raw is false */ public function getLuma($raw = false) { return $raw ? $this->color->getLuma() : new ILess_Node_Dimension(ILess_Math::clean(ILess_Math::multiply(ILess_Math::round(ILess_Math::multiply($this->color->getLuma(), $this->color->getAlpha()), 2), 100)), '%'); }
/** * Returns the luma * * @return integer */ public function getLuma() { if ($this->luma !== null) { return $this->luma; } // Y = 0.2126 R + 0.7152 G + 0.0722 B $r = ILess_Math::multiply('0.2126', ILess_Math::divide($this->rgb[0], 255)); $g = ILess_Math::multiply('0.7152', ILess_Math::divide($this->rgb[1], 255)); $b = ILess_Math::multiply('0.0722', ILess_Math::divide($this->rgb[2], 255)); $this->luma = ILess_Math::add(ILess_Math::add($r, $g), $b); return $this->luma; }
/** * 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); }