How does colorised texture work?


#1

Hi,

I want to know how colorised texture works exactly.
I read that the texture is first converted to gray scale and then the color is applied on top of it.
I tried doing that, multiplying color value with gray scale texture color values. This doesn’t work.

I also read that same hue from color is applied to the gray scale texture. Can anyone explain this?
Do we need to add colors in HSL space?


#2

A search of the developer categories on this subject:

The answer I like best comes from Andreas:


#3

Hi Dan,
I went through the above link.

But, still it is not clear to me.

This is my understanding:
In case of Colorized Texture, the texture is first converted to gray scale and then the color value is multiplied with the gray scale values.

Is this correct?


#4

I don’t think it’s a tinted grayscale image. It looks bore like saturation and lightness are kept while the hue for each pixel is changed.


#5

We should probably document this in a page in the documentation. Let start here in making the first attempt - let me know if you need more details.

(EDIT: We now have documented it: https://github.com/SketchUp/sketchup-colorize-algorithm)

SketchUp perform two types of color adjustments:

If you set a material color for a textured material via the Ruby API’s Sketchup::Material.color= it sets the type to Sketchup::Material::COLORIZE_TINT.

You can toggle the type using Sketchup::Material.colorize_type=

The UI have a checkbox:

image

For what is happening on our end:

  • Each pixel is processed one by one.
  • RGB value is converted to HLS.

Sketchup::Material::COLORIZE_SHIFT

  • Adjustment Hue is added to Pixel Hue
  • Hue ensured to be within 0-360 range by rolling around.
  • Adjustment Saturation is added to Pixel Saturation (clamped to 0.0-1.0).
  • Adjustment Lumination is added to Pixel Lumination (clamped to 0.01-1.0).

Sketchup::Material::COLORIZE_TINT

  • Pixel Hue is set to Adjustment Hue
  • Hue ensured to be within 0-360 range by rolling around.
  • Adjustment Saturation is added to Pixel Saturation (clamped to 0.0-1.0).
  • Adjustment Lumination is added to Pixel Lumination (clamped to 0.01-1.0).

Converting back to RGB
If the resulting Pixel Saturation is 0.0 the RGB is set to the Pixel Lumination
If not, regular HLS to RGB conversion

Maybe I should write this out into pseudo code or Ruby code…

Btw, I just noticed that the C API is lacking the properties to determine the deltas and colorization types. That will need to be added.


Color, Texture, Both?
Some questions about Textured material and ColorizedTexture material
#6

And I assume the adjustment hue is the difference between the avarage hue of the original image and the hue of the color the user defined?


#7

Yes - good catch. The color delta is used to adjust. It’s computed from the texture’s average color and the adjustment color.

Describing how the delta is derived is much easier if I just write it in pseudo-code. (I’ll have to get back on this - maybe tomorrow.) The Ruby API have a method for this already: Sketchup::Material.colorize_deltas http://ruby.sketchup.com/Sketchup/Material.html#colorize_deltas-instance_method

Again the C API is lacking that property.


#8

Note that using the Ruby API you can export colorized textures since SU2016: Sketchup::Texture#write


#9

Thank You Thomas.

This is very helpful.

Is there any other way we can know the colorized type (tint or shift) from C API ?


#10

Afraid not - this was a long standing issue with the Ruby API - not being able to know the exact state of the material.
We’re constantly working on fleshing out the C API though - so it will come.


#11

Hi Thomas,

As you were saying, can you explain the color delta calculation using pseudo code?

Thank You


#12

Sorry - things got really busy. Here’s a quick draft:

def get_deltas(shift = true)
  from = texture.average_color
  to = material.color
  
  # Get HLS of base color.
  base_h, base_s, base_l = from.get_hsl

  # Get HLS of new color.
  h, s, l = to.get_hsl
  
  if to.is_monochrome? && from.is_monochrome?
    h = 0
    s = 0
    l = l - base_l
  else if to.is_monochrome?
    h = 0
    s = -1
    l = l - base_l
  else if from.is_monochrome?
    l = l - base_l
  else
    l = l - base_l
    s = s - base_s
    if shift
      h = h - base_h
    end
  end
    
  return [h, s, l]
end

#13

Thank you Thomas, this helped a lot.

the pseudo code for calculating deltas and the actual logic of COLORIZE_SHIFT and COLORIZE_TINT you provided gives the full picture of colorized_texture.

Only problem is, Since I am using C SDK, I can’t differentiate between COLORIZE_SHIFT and COLORIZE_TINT.
For now, I assumed it is COLORIZE_TINT by default.


#14

In the GUI shift is the default and you have to actively tick a checkbox to get tint. I can’t say what is the most common though.


#15

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.


#16

For the record, we have documented the logic in a GitHub repository: https://github.com/SketchUp/sketchup-colorize-algorithm