PHP allocate color without image resource

前端 未结 4 952
孤独总比滥情好
孤独总比滥情好 2020-12-04 03:08

Can you allocate a color in PHP GD without an image resource? It should be possible because really an allocated color is a number, right?

$im = imagecreatet         


        
4条回答
  •  忘掉有多难
    2020-12-04 03:12

    The color index, or color id can be determined several ways. This should help on the road to discovery.

    Update *** I'm not sure why, but after seeing this question I wrote the following function to convert types of color values to types of color expressions. Hope it works for someone. *Added CMYK just because. ...

    Options --> 'int', 'rgba', 'rgb', 'cmyk', 'hex', 'rgbaCSS', 'rgbCSS', 'hexCSS', 'hexCSS4'

    If someone wanted to get really serious, they could write optional arguments for converting the format to any; using sprintf.

     2) { return $colorValue; }
        if (!is_array($colorValue)) {
            // OUT --> {Mixed} :: Options --> rgba || rgb || cmyk
            // If not an array then we have some form of Number - Valid values int || hex
            // Please note that hex numbers such as 0x000000 that are not strings '0x000000' are interpreted by php as their number representation.
            // Shortcode hex values.
            if (is_string($colorValue) && $colorValue{0} === '#') {
                $start = 0;
                if (strlen($colorValue) === 4) {
                    $rgb['alpha'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
                } else {
                    $rgb['alpha'] = 0;
                }
                $rgb['r'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
                $rgb['g'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
                $rgb['b'] = hexdec(str_repeat(substr($colorValue, $start, 1), 2));
                // We need to make sure this follows some rules before we send it back even if it is the type we are being asked for.
                return convertColorValue($rgb, $toType);
            }
            // Make sure we have a clean sensible string for conversion.
            if (preg_match("/[^0-9a-f]/i", $colorValue) !== false) {
                if (($colorValue{0} === 0 && $colorValue{1} !== 'x'))
                    $colorValue = '0x' . $colorValue;
            }
            $colorValue = preg_replace("/[^0-9a-fx]/i", '', $colorValue); // 4294967295 === 0xFFFFFFFF, the maximum color value.
            $colorValue = strlen((string) $colorValue) >= 10 ? (intval(substr($colorValue, 0, 10), 0) > 4294967295 ? 0 : substr($colorValue, 0, 10)) : $colorValue;
            // CONVERT our number to base 10 from whatever base it is in.
            // **** TODO: Hopefully int or hex, not sure what happens in other bases. ¯\_(ツ)_/¯
            // Hex strings should always be prepended with 0x, otherwise confusion happens 11110000 !=? 0x11110000
            // If not prepended here with 0x, I can't fix/predict which you are trying to represent int or hex. Int is naturally assumed.
            $color = [];
            $colorValue = intval(intval($colorValue, 0), 10);
            $color['r'] = ($colorValue >> 16) & 0xFF;
            $color['g'] = ($colorValue >> 8) & 0xFF;
            $color['b'] = $colorValue & 0xFF;
            $color['alpha'] = ($colorValue >> 24) & 0x7F;
            if ($toType === 'cmyk') {
                $c = (255 - $color['r']) / 255.0 * 100;
                $m = (255 - $color['g']) / 255.0 * 100;
                $y = (255 - $color['b']) / 255.0 * 100;
                $b = min([$c,$m,$y]);
                $c = $c - $b; $m = $m - $b; $y = $y - $b;
                return ['c' => $c, 'm' => $m, 'y' => $y, 'k' => $b, 'alpha' => $color['alpha']];
            }
            if ($toType === 'rgba' || $toType === 'rgb') {
                return $color;
            }
            return convertColorValue($color, $toType, ++$recurse);
        }
        // OUT --> {Mixed} :: Options --> int || hex || hexCSS || rgbCSS || rgbaCSS || hexCSS4
        // If they've thrown us a c, we are going to assume they gave us CMYK.
        if (isset($colorValue['c'])) {
            $colorValue['c'] = empty($colorValue['c']) ? 0 : $colorValue['c'] / 100;
            $colorValue['m'] = empty($colorValue['m']) ? 0 : $colorValue['m'] / 100;
            $colorValue['y'] = empty($colorValue['y']) ? 0 : $colorValue['y'] / 100;
            $colorValue['k'] = empty($colorValue['k']) ? 0 : $colorValue['k'] / 100;
            $colorValue['r'] = round((1 - ($colorValue['c'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
            $colorValue['g'] = round((1 - ($colorValue['m'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
            $colorValue['b'] = round((1 - ($colorValue['y'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
    
        } else {
            $colorValue['r'] = empty($colorValue['r']) ? 0 : $colorValue['r'];
            $colorValue['g'] = empty($colorValue['g']) ? 0 : $colorValue['g'];
            $colorValue['b'] = empty($colorValue['b']) ? 0 : $colorValue['b'];
        }
        $colorValue['alpha'] = (empty($colorValue['alpha']) || $colorValue['alpha'] > 0x7f) ? 0 : $colorValue['alpha'];
        if ($toType === 'rgbaCSS') {
            $color['alpha'] = empty($color['alpha']) ? 1 :
                (round(1 * $color['alpha'] / 127, 3, PHP_ROUND_HALF_DOWN));
            return "rgba({$colorValue['r']}, {$colorValue['g']}, {$colorValue['b']}, {$colorValue['alpha']})";
        }
        if ($toType === 'rgbCSS') {
            return "rgb({$colorValue['r']}, {$colorValue['g']}, {$colorValue['b']})";
        }
        // Just return the rgb value if opaque since '==' color-values.
        if (empty($colorValue['alpha'])) {
            $hex = sprintf("%02x%02x%02x", $colorValue['r'], $colorValue['g'], $colorValue['b']);
        } else {
            $hex = sprintf("%02x%02x%02x%02x", $colorValue['alpha'], $colorValue['r'], $colorValue['g'], $colorValue['b']);
        }
        $isCSS = ($toType === 'hexCSS4' || $toType === 'hexCSS');
        if ($toType === 'hex' || $isCSS) {
            return $isCSS ? ("#".$hex) : ("0x".$hex);
        }
        $hex = "0x".$hex;
        if ($toType === 'int') { return hexdec($hex); }
        // Is now a hex string. Going back UP^
        return convertColorValue($hex, $toType, ++$recurse);
    }
    

提交回复
热议问题