ColorThief\VBox::avg PHP Method

avg() public method

public avg ( $force = false )
    public function avg($force = false)
    {
        if (!$this->avg || $force) {
            $ntot = 0;
            $mult = 1 << 8 - ColorThief::SIGBITS;
            $rsum = 0;
            $gsum = 0;
            $bsum = 0;
            for ($i = $this->r1; $i <= $this->r2; $i++) {
                for ($j = $this->g1; $j <= $this->g2; $j++) {
                    for ($k = $this->b1; $k <= $this->b2; $k++) {
                        $histoindex = ColorThief::getColorIndex($i, $j, $k);
                        $hval = isset($this->histo[$histoindex]) ? $this->histo[$histoindex] : 0;
                        $ntot += $hval;
                        $rsum += $hval * ($i + 0.5) * $mult;
                        $gsum += $hval * ($j + 0.5) * $mult;
                        $bsum += $hval * ($k + 0.5) * $mult;
                    }
                }
            }
            if ($ntot) {
                $this->avg = array(~~($rsum / $ntot), ~~($gsum / $ntot), ~~($bsum / $ntot));
            } else {
                // echo 'empty box'."\n";
                $this->avg = array(~~($mult * ($this->r1 + $this->r2 + 1) / 2), ~~($mult * ($this->g1 + $this->g2 + 1) / 2), ~~($mult * ($this->b1 + $this->b2 + 1) / 2));
            }
        }
        return $this->avg;
    }