View file upload/src/addons/AddonFlare/PaidRegistrations/Repository/AccountType.php

File size: 1.9Kb
<?php

namespace AddonFlare\PaidRegistrations\Repository;

use XF\Mvc\Entity\Finder;
use XF\Mvc\Entity\Repository;

class AccountType extends Repository
{
    public function findAccountTypesForList()
    {
        $finder = $this->finder('AddonFlare\PaidRegistrations:AccountType')
            ->with('UserUpgrade')
            ->order('active', 'DESC')
            ->order('display_order', 'ASC');

        return $finder;
    }

    public function findActiveAccountTypesForList()
    {
        $finder = $this->findAccountTypesForList()
            ->where('active', '=', 1)
            ->whereOr(['UserUpgrade.can_purchase', '=', 1], ['user_upgrade_id', '=', -1]);

        return $finder;
    }

    public function rgbToHex($rgb)
    {
        $re = '/\((\d+)[ ,]*(\d+)[ ,]*(\d+)/';
        if (preg_match($re, $rgb, $match))
        {
            $hex = sprintf("#%02x%02x%02x", $match[1], $match[2], $match[3]);
        }
        else
        {
            $hex = '';
        }

        return $hex;
    }

    public function adjustBrightness($hex, $steps) // credit: https://stackoverflow.com/a/11951022
    {
        // Steps should be between -255 and 255. Negative = darker, positive = lighter
        $steps = max(-255, min(255, $steps));

        // Normalize into a six character long hex string
        $hex = str_replace('#', '', $hex);
        if (strlen($hex) == 3) {
            $hex = str_repeat(substr($hex,0,1), 2).str_repeat(substr($hex,1,1), 2).str_repeat(substr($hex,2,1), 2);
        }

        // Split into three parts: R, G and B
        $color_parts = str_split($hex, 2);
        $return = '#';

        foreach ($color_parts as $color) {
            $color   = hexdec($color); // Convert to decimal
            $color   = max(0,min(255,$color + $steps)); // Adjust color
            $return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code
        }

        return $return;
    }
}