4
$\begingroup$

So we can see that Hue is a periodic function. Its period is 1. And the specifier from documentation. Sorry for I cannot cite my local language documentation. Let me take a look the normal situation:

Graphics @ {Hue[-0.33, 1, 1], Disk[]}

enter image description here

But how about this?

Image[{{{-0.33, 1, 1}}}, ColorSpace -> "HSB"]

enter image description here

Update the question like this LLlAMnYP, we can see the data will not be adjust to 0~1:

enter image description here

$\endgroup$
4
  • $\begingroup$ re: the update, I don't see this as a contradiction. If Image interprets the data in a certain way, there is no need to introduce unnecessary overhead by transforming the data to a "canonical" form. $\endgroup$
    – LLlAMnYP
    Commented Oct 14, 2015 at 7:19
  • $\begingroup$ On the other hand, this has implications, that come up when using various functions like ImageSubtract. I'll add an example to my answer. $\endgroup$
    – LLlAMnYP
    Commented Oct 14, 2015 at 7:27
  • $\begingroup$ Yeah,I'm look forward to your example,and sorry my poor English that I can understand your "overhead",and for this case,how can I let the pixed of {-0.33,1,1} to display a correct color of Blue? $\endgroup$
    – yode
    Commented Oct 14, 2015 at 7:32
  • $\begingroup$ I've edited my answer, See also Jason's input. $\endgroup$
    – LLlAMnYP
    Commented Oct 14, 2015 at 7:44

2 Answers 2

10
$\begingroup$

So LLlAMnYP answered why it displays red instead of blue, but it got me to thinking how to make it behave the way OP expected it to.

Here is a comparison of Hue inside of Image and just by itself:

TableForm[
{Table[{n, Image[{{{n, 1, 1}}}, ColorSpace -> "HSB"],
 Graphics[{Hue[n, 1, 1], Rectangle[]}, ImageSize -> 22]}, {n, -2, 0, 0.1}], 
   Table[{n, Image[{{{n, 1, 1}}}, ColorSpace -> "HSB"], 
   Graphics[{Hue[n, 1, 1], Rectangle[]}, ImageSize -> 22]}, {n, 0, 2, 0.1}]}
   ]

enter image description here

So to get it to behave properly, we use Mod, with a slight offset so that it doesn't set a brightness value of 1 equal to 0:

TableForm[
 {Table[{n, Image[Mod[{{{n, 1, 1}}}, 1, .001], ColorSpace -> "HSB"],
     Graphics[{Hue[n, 1, 1], Rectangle[]}, ImageSize -> 22]}, {n, -2, 
    0, 0.1}], 
     Table[{n, 
    Image[Mod[{{{n, 1, 1}}}, 1, .001], ColorSpace -> "HSB"], 
       Graphics[{Hue[n, 1, 1], Rectangle[]}, ImageSize -> 22]}, {n, 
    0, 2, 0.1}]}
    ]

enter image description here

and your original example is simply

Image[Mod[{{{-0.33, 1, 1}}}, 1, .001], ColorSpace -> "HSB"]

enter image description here

$\endgroup$
4
  • $\begingroup$ +1, we think alike. As far as my preference goes, Hue may be periodic, but brightness and saturation have no reason to be, so I apply Mod only to the hue channel. And thanks to periodicity, transforming Hue of 1 to 0 isn't much of a problem. $\endgroup$
    – LLlAMnYP
    Commented Oct 14, 2015 at 7:44
  • $\begingroup$ Yeah - I really wanted to be able to apply the change to the ColorSpace itself, as in ColorSpace->Hue[Mod[#1,1],#2,#3]& but I can't find any documentation on using a user-defined ColorSpace. You can put a ColorFunction there, as in ColorSpace->Hue, but there seems to be some limit. $\endgroup$
    – Jason B.
    Commented Oct 14, 2015 at 7:55
  • $\begingroup$ It appears, that the valid option values for ColorSpace are quite smart, it accepts RGB; "RGB", "Hue", "HSB", HSB, Hue etc. No matter, strings or not, some aren't even defined symbols. I guess it's not intended to take functions, but just strings/symbols of a specific syntax. $\endgroup$
    – LLlAMnYP
    Commented Oct 14, 2015 at 8:28
  • $\begingroup$ But regardless of the specification, ImageColorSpace always then returns the color space in a proper string form ("HSB", "RGB", etc). $\endgroup$
    – LLlAMnYP
    Commented Oct 14, 2015 at 8:29
9
$\begingroup$

Hue is a periodic function. However, from the documentation:

Image[data,"type"] coerces values in data to the specified type by rounding or clipping.

So as argument to image, negative hues are equivalent to 0.

Re: comment by Jason B.
This is even more explicitly explained in two nearby lines of the documentation:

Image[data] is equivalent to Image[data,"Real"].
Image[data] by default allows any real number, but displays only values between 0 and 1.

@Yode notes, that the ImageData itself is not clipped to 0-1. At first glance, this doesn't matter, as clipped or not, the output result will be the same. However, consider

{im1, im2} = {Image[{{{-.5, .5, .5}}}, ColorSpace -> "HSB"],
                   Image[{{{0., .5, .5}}}, ColorSpace -> "HSB"]}

pixels (* Identical appearance, differing ImageData *)

im3 = Image[{{{0., 1., 1.}}}, ColorSpace -> "HSB"]

pixels2

{ImageSubtract[im3,im1], ImageSubtract[im3,im2]}

pixels3

Thus we subtract from the same image images of identical appearance and get different results. I feel, it's not exactly desirable behavior.

Re: follow-up question in comment:

data = {{{-.33, 1., 1.}}}
Apply[{Mod[#1, 1], #2, #3} &, data, {2}]
Image[Apply[{Mod[#1, 1], #2, #3} &, data, {2}], ColorSpace -> "HSB"]

result

Here's a better approach using specialized image functions (ImageApply):

im1 = Image[data, ColorSpace -> "HSB"]
ImageApply[{Mod[First@#, 1]}~Join~Rest@# &, im1]

pixels5

$\endgroup$
4
  • $\begingroup$ In this case, OP isn't specifying a "type", but it also says that "Image[data] by default allows any real number, but displays only values between 0 and 1." $\endgroup$
    – Jason B.
    Commented Oct 14, 2015 at 7:14
  • $\begingroup$ Thanks for your answer and I'll update my question to spectify the case. $\endgroup$
    – yode
    Commented Oct 14, 2015 at 7:17
  • $\begingroup$ @JasonB duly noted, thanks. $\endgroup$
    – LLlAMnYP
    Commented Oct 14, 2015 at 7:21
  • $\begingroup$ I'm verry moving to your help and answer like so careful.But the Jason B's solution is more concise I think.And I cannot accept two answer.Thank you once again. $\endgroup$
    – yode
    Commented Oct 14, 2015 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.