/usr/lib/pypy/dist-packages/wand/color.py is in pypy-wand 0.3.8-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | """:mod:`wand.color` --- Colors
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. versionadded:: 0.1.2
"""
import ctypes
from .api import MagickPixelPacket, library
from .compat import binary, text
from .resource import Resource
from .version import QUANTUM_DEPTH
__all__ = 'Color', 'scale_quantum_to_int8'
class Color(Resource):
"""Color value.
Unlike any other objects in Wand, its resource management can be
implicit when it used outside of :keyword:`with` block. In these case,
its resource are allocated for every operation which requires a resource
and destroyed immediately. Of course it is inefficient when the
operations are much, so to avoid it, you should use color objects
inside of :keyword:`with` block explicitly e.g.::
red_count = 0
with Color('#f00') as red:
with Image(filename='image.png') as img:
for row in img:
for col in row:
if col == red:
red_count += 1
:param string: a color namel string e.g. ``'rgb(255, 255, 255)'``,
``'#fff'``, ``'white'``. see `ImageMagick Color Names`_
doc also
:type string: :class:`basestring`
.. versionchanged:: 0.3.0
:class:`Color` objects become hashable.
.. seealso::
`ImageMagick Color Names`_
The color can then be given as a color name (there is a limited
but large set of these; see below) or it can be given as a set
of numbers (in decimal or hexadecimal), each corresponding to
a channel in an RGB or RGBA color model. HSL, HSLA, HSB, HSBA,
CMYK, or CMYKA color models may also be specified. These topics
are briefly described in the sections below.
.. _ImageMagick Color Names: http://www.imagemagick.org/script/color.php
.. describe:: == (other)
Equality operator.
:param other: a color another one
:type color: :class:`Color`
:returns: ``True`` only if two images equal.
:rtype: :class:`bool`
"""
c_is_resource = library.IsPixelWand
c_destroy_resource = library.DestroyPixelWand
c_get_exception = library.PixelGetException
c_clear_exception = library.PixelClearException
__slots__ = 'raw', 'c_resource', 'allocated'
def __init__(self, string=None, raw=None):
if (string is None and raw is None or
string is not None and raw is not None):
raise TypeError('expected one argument')
self.allocated = 0
if raw is None:
self.raw = ctypes.create_string_buffer(
ctypes.sizeof(MagickPixelPacket)
)
with self:
library.PixelSetColor(self.resource, binary(string))
library.PixelGetMagickColor(self.resource, self.raw)
else:
self.raw = raw
def __getinitargs__(self):
return self.string, None
def __enter__(self):
if not self.allocated:
with self.allocate():
self.resource = library.NewPixelWand()
library.PixelSetMagickColor(self.resource, self.raw)
self.allocated += 1
return Resource.__enter__(self)
def __exit__(self, type, value, traceback):
self.allocated -= 1
if not self.allocated:
Resource.__exit__(self, type, value, traceback)
@property
def string(self):
"""(:class:`basestring`) The string representation of the color."""
with self:
color_string = library.PixelGetColorAsString(self.resource)
return text(color_string.value)
@property
def normalized_string(self):
"""(:class:`basestring`) The normalized string representation of
the color. The same color is always represented to the same
string.
.. versionadded:: 0.3.0
"""
with self:
string = library.PixelGetColorAsNormalizedString(self.resource)
return text(string.value)
@staticmethod
def c_equals(a, b):
"""Raw level version of equality test function for two pixels.
:param a: a pointer to PixelWand to compare
:type a: :class:`ctypes.c_void_p`
:param b: a pointer to PixelWand to compare
:type b: :class:`ctypes.c_void_p`
:returns: ``True`` only if two pixels equal
:rtype: :class:`bool`
.. note::
It's only for internal use. Don't use it directly.
Use ``==`` operator of :class:`Color` instead.
"""
alpha = library.PixelGetAlpha
return bool(library.IsPixelWandSimilar(a, b, 0) and
alpha(a) == alpha(b))
def __eq__(self, other):
if not isinstance(other, Color):
return False
with self as this:
with other:
return self.c_equals(this.resource, other.resource)
def __ne__(self, other):
return not (self == other)
def __hash__(self):
if self.alpha:
return hash(self.normalized_string)
return hash(None)
@property
def red(self):
"""(:class:`numbers.Real`) Red, from 0.0 to 1.0."""
with self:
return library.PixelGetRed(self.resource)
@property
def green(self):
"""(:class:`numbers.Real`) Green, from 0.0 to 1.0."""
with self:
return library.PixelGetGreen(self.resource)
@property
def blue(self):
"""(:class:`numbers.Real`) Blue, from 0.0 to 1.0."""
with self:
return library.PixelGetBlue(self.resource)
@property
def alpha(self):
"""(:class:`numbers.Real`) Alpha value, from 0.0 to 1.0."""
with self:
return library.PixelGetAlpha(self.resource)
@property
def red_quantum(self):
"""(:class:`numbers.Integral`) Red.
Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.
.. versionadded:: 0.3.0
"""
with self:
return library.PixelGetRedQuantum(self.resource)
@property
def green_quantum(self):
"""(:class:`numbers.Integral`) Green.
Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.
.. versionadded:: 0.3.0
"""
with self:
return library.PixelGetGreenQuantum(self.resource)
@property
def blue_quantum(self):
"""(:class:`numbers.Integral`) Blue.
Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.
.. versionadded:: 0.3.0
"""
with self:
return library.PixelGetBlueQuantum(self.resource)
@property
def alpha_quantum(self):
"""(:class:`numbers.Integral`) Alpha value.
Scale depends on :const:`~wand.version.QUANTUM_DEPTH`.
.. versionadded:: 0.3.0
"""
with self:
return library.PixelGetAlphaQuantum(self.resource)
@property
def red_int8(self):
"""(:class:`numbers.Integral`) Red as 8bit integer which is a common
style. From 0 to 255.
.. versionadded:: 0.3.0
"""
return scale_quantum_to_int8(self.red_quantum)
@property
def green_int8(self):
"""(:class:`numbers.Integral`) Green as 8bit integer which is
a common style. From 0 to 255.
.. versionadded:: 0.3.0
"""
return scale_quantum_to_int8(self.green_quantum)
@property
def blue_int8(self):
"""(:class:`numbers.Integral`) Blue as 8bit integer which is
a common style. From 0 to 255.
.. versionadded:: 0.3.0
"""
return scale_quantum_to_int8(self.blue_quantum)
@property
def alpha_int8(self):
"""(:class:`numbers.Integral`) Alpha value as 8bit integer which is
a common style. From 0 to 255.
.. versionadded:: 0.3.0
"""
return scale_quantum_to_int8(self.alpha_quantum)
def __str__(self):
return self.string
def __repr__(self):
c = type(self)
return '{0}.{1}({2!r})'.format(c.__module__, c.__name__, self.string)
def scale_quantum_to_int8(quantum):
"""Straightforward port of :c:func:`ScaleQuantumToChar()` inline
function.
:param quantum: quantum value
:type quantum: :class:`numbers.Integral`
:returns: 8bit integer of the given ``quantum`` value
:rtype: :class:`numbers.Integral`
.. versionadded:: 0.3.0
"""
if quantum <= 0:
return 0
table = {8: 1, 16: 257.0, 32: 16843009.0, 64: 72340172838076673.0}
v = quantum / table[QUANTUM_DEPTH]
if v >= 255:
return 255
return int(v + 0.5)
|