github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/resources/images/filters.go (about)

     1  // Copyright 2019 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Package images provides template functions for manipulating images.
    15  package images
    16  
    17  import (
    18  	"github.com/disintegration/gift"
    19  	"github.com/spf13/cast"
    20  )
    21  
    22  // Increment for re-generation of images using these filters.
    23  const filterAPIVersion = 0
    24  
    25  type Filters struct {
    26  }
    27  
    28  // Overlay creates a filter that overlays src at position x y.
    29  func (*Filters) Overlay(src ImageSource, x, y interface{}) gift.Filter {
    30  	return filter{
    31  		Options: newFilterOpts(src.Key(), x, y),
    32  		Filter:  overlayFilter{src: src, x: cast.ToInt(x), y: cast.ToInt(y)},
    33  	}
    34  }
    35  
    36  // Brightness creates a filter that changes the brightness of an image.
    37  // The percentage parameter must be in range (-100, 100).
    38  func (*Filters) Brightness(percentage interface{}) gift.Filter {
    39  	return filter{
    40  		Options: newFilterOpts(percentage),
    41  		Filter:  gift.Brightness(cast.ToFloat32(percentage)),
    42  	}
    43  }
    44  
    45  // ColorBalance creates a filter that changes the color balance of an image.
    46  // The percentage parameters for each color channel (red, green, blue) must be in range (-100, 500).
    47  func (*Filters) ColorBalance(percentageRed, percentageGreen, percentageBlue interface{}) gift.Filter {
    48  	return filter{
    49  		Options: newFilterOpts(percentageRed, percentageGreen, percentageBlue),
    50  		Filter:  gift.ColorBalance(cast.ToFloat32(percentageRed), cast.ToFloat32(percentageGreen), cast.ToFloat32(percentageBlue)),
    51  	}
    52  }
    53  
    54  // Colorize creates a filter that produces a colorized version of an image.
    55  // The hue parameter is the angle on the color wheel, typically in range (0, 360).
    56  // The saturation parameter must be in range (0, 100).
    57  // The percentage parameter specifies the strength of the effect, it must be in range (0, 100).
    58  func (*Filters) Colorize(hue, saturation, percentage interface{}) gift.Filter {
    59  	return filter{
    60  		Options: newFilterOpts(hue, saturation, percentage),
    61  		Filter:  gift.Colorize(cast.ToFloat32(hue), cast.ToFloat32(saturation), cast.ToFloat32(percentage)),
    62  	}
    63  }
    64  
    65  // Contrast creates a filter that changes the contrast of an image.
    66  // The percentage parameter must be in range (-100, 100).
    67  func (*Filters) Contrast(percentage interface{}) gift.Filter {
    68  	return filter{
    69  		Options: newFilterOpts(percentage),
    70  		Filter:  gift.Contrast(cast.ToFloat32(percentage)),
    71  	}
    72  }
    73  
    74  // Gamma creates a filter that performs a gamma correction on an image.
    75  // The gamma parameter must be positive. Gamma = 1 gives the original image.
    76  // Gamma less than 1 darkens the image and gamma greater than 1 lightens it.
    77  func (*Filters) Gamma(gamma interface{}) gift.Filter {
    78  	return filter{
    79  		Options: newFilterOpts(gamma),
    80  		Filter:  gift.Gamma(cast.ToFloat32(gamma)),
    81  	}
    82  }
    83  
    84  // GaussianBlur creates a filter that applies a gaussian blur to an image.
    85  func (*Filters) GaussianBlur(sigma interface{}) gift.Filter {
    86  	return filter{
    87  		Options: newFilterOpts(sigma),
    88  		Filter:  gift.GaussianBlur(cast.ToFloat32(sigma)),
    89  	}
    90  }
    91  
    92  // Grayscale creates a filter that produces a grayscale version of an image.
    93  func (*Filters) Grayscale() gift.Filter {
    94  	return filter{
    95  		Filter: gift.Grayscale(),
    96  	}
    97  }
    98  
    99  // Hue creates a filter that rotates the hue of an image.
   100  // The hue angle shift is typically in range -180 to 180.
   101  func (*Filters) Hue(shift interface{}) gift.Filter {
   102  	return filter{
   103  		Options: newFilterOpts(shift),
   104  		Filter:  gift.Hue(cast.ToFloat32(shift)),
   105  	}
   106  }
   107  
   108  // Invert creates a filter that negates the colors of an image.
   109  func (*Filters) Invert() gift.Filter {
   110  	return filter{
   111  		Filter: gift.Invert(),
   112  	}
   113  }
   114  
   115  // Pixelate creates a filter that applies a pixelation effect to an image.
   116  func (*Filters) Pixelate(size interface{}) gift.Filter {
   117  	return filter{
   118  		Options: newFilterOpts(size),
   119  		Filter:  gift.Pixelate(cast.ToInt(size)),
   120  	}
   121  }
   122  
   123  // Saturation creates a filter that changes the saturation of an image.
   124  func (*Filters) Saturation(percentage interface{}) gift.Filter {
   125  	return filter{
   126  		Options: newFilterOpts(percentage),
   127  		Filter:  gift.Saturation(cast.ToFloat32(percentage)),
   128  	}
   129  }
   130  
   131  // Sepia creates a filter that produces a sepia-toned version of an image.
   132  func (*Filters) Sepia(percentage interface{}) gift.Filter {
   133  	return filter{
   134  		Options: newFilterOpts(percentage),
   135  		Filter:  gift.Sepia(cast.ToFloat32(percentage)),
   136  	}
   137  }
   138  
   139  // Sigmoid creates a filter that changes the contrast of an image using a sigmoidal function and returns the adjusted image.
   140  // It's a non-linear contrast change useful for photo adjustments as it preserves highlight and shadow detail.
   141  func (*Filters) Sigmoid(midpoint, factor interface{}) gift.Filter {
   142  	return filter{
   143  		Options: newFilterOpts(midpoint, factor),
   144  		Filter:  gift.Sigmoid(cast.ToFloat32(midpoint), cast.ToFloat32(factor)),
   145  	}
   146  }
   147  
   148  // UnsharpMask creates a filter that sharpens an image.
   149  // The sigma parameter is used in a gaussian function and affects the radius of effect.
   150  // Sigma must be positive. Sharpen radius roughly equals 3 * sigma.
   151  // The amount parameter controls how much darker and how much lighter the edge borders become. Typically between 0.5 and 1.5.
   152  // The threshold parameter controls the minimum brightness change that will be sharpened. Typically between 0 and 0.05.
   153  func (*Filters) UnsharpMask(sigma, amount, threshold interface{}) gift.Filter {
   154  	return filter{
   155  		Options: newFilterOpts(sigma, amount, threshold),
   156  		Filter:  gift.UnsharpMask(cast.ToFloat32(sigma), cast.ToFloat32(amount), cast.ToFloat32(threshold)),
   157  	}
   158  }
   159  
   160  type filter struct {
   161  	Options filterOpts
   162  	gift.Filter
   163  }
   164  
   165  // For cache-busting.
   166  type filterOpts struct {
   167  	Version int
   168  	Vals    interface{}
   169  }
   170  
   171  func newFilterOpts(vals ...interface{}) filterOpts {
   172  	return filterOpts{
   173  		Version: filterAPIVersion,
   174  		Vals:    vals,
   175  	}
   176  }