📄 color.class
字号:
/* color
Class for storing and managing colors
Note that .setting, but not .stylename and .snm returns a string
containing the rgb values, rather than the name.
*/
*! version 1.0.2 05dec2003
version 8
class {
instance:
rgb = "255 255 255"
_type = .gditype.new, set(line)
} , inherit(style)
/* -------------------------------------------------------------------------*/
/*
Usage: .new [, style(name) rgb(#_r #_g #_b) hsb(#_hue #_sat #_bright)
color(clr_name) rgbhex(hexstring) type(line|...)]
note, color(clr_name) is just a synonym for .setstyle.
*/
program define new
syntax [ , Style(passthru) * ]
if `"`options'"' != `""' {
.set , `options'
}
if `"`style'"' != "" {
.Super.new , `style'
}
end
/* -------------------------------------------------------------------------*/
/*
Usage: just like .new
*/
program set
syntax [ , Color(string) HSB(numlist min=3 max=3) ///
RGB(numlist integer min=3 max=3 >=0 <=255) ///
CMYK(numlist integer min=4 max=4 >=0 <=255) ///
RGBHex(string) Style(string) Type(string) ]
if "`type'" != "" {
._type.set `type'
}
if "`rgb'" != "" {
.rgb = "`rgb'"
exit
}
if "`hsb'" != "" {
.sethsb "`hsb'"
exit
}
if "`cmyk'" != "" {
.setcmyk "`hsb'"
exit
}
if "`color'" != "" {
.setstyle , style(`color')
exit
}
if "`style'" != "" {
.setstyle , style(`style')
exit
}
if "`rgbhex'" != "" {
.setrgbhex "`rgbhex'"
exit
}
end
/* -------------------------------------------------------------------------*/
/* Allow a series of 3 numbers (#_red, #_green, #_blue) in place
of a stylename to mean "set the rgb value of the current style to these
values.
Usage: .a.b.setstyle, style(#|stylename|scheme)
*/
program setstyle
// accept RGB numbers
capture syntax , Style(numlist min=3 max=3 integer >=0 <=255)
if ! _rc {
.remake_as_copy
.rgb = "`style'"
exit
}
// accept CMYK numbers
capture syntax , Style(numlist min=4 max=4 >=0 <=255)
if ! _rc {
.remake_as_copy
capture .setcmyk `style'
if (! _rc) exit
}
syntax [ , Style(string) ] // Allow scheme to set value
local scheme_setting `.`c(curscm)'.style `.classname' `style''
capture numlist "`scheme_setting'", min(3) max(3) integer ///
range(>=0 <=255)
if ! _rc { // RGB
local rgb `r(numlist)'
.remake_as_copy
.rgb = `"`rgb'"'
exit // Exit
}
capture numlist "`scheme_setting'", min(4) max(4) range(>=0 <=255)
if ! _rc { // CMYK
local cmyk `r(numlist)'
.remake_as_copy
capture .setcmyk `r(numlist)'
if (! _rc) exit // Exit
}
// allow +/- for intensity
gettoken op num : style , parse("+-")
if "`op'" == "+" | "`op'" == "-" {
capture confirm number `num'
if !_rc {
.remake_as_copy // OK to run twice
foreach v in `.rgb' {
local v = round(`v' * (1 `op' `num'/100), 1)
local v = max(0, min(255, int(`v')))
local rgb `rgb' `v'
}
.rgb = "`rgb'"
exit // Exit
}
}
// *<#_inten>
gettoken op inten : style , parse("*")
if `"`op'"' == `"*"' {
.remake_as_copy
.setintensity `inten'
exit // Exit
}
// <style/scheme style/r g b>*<#_inten>
gettoken prestyle rest : style , parse("*")
gettoken op inten : rest , parse("* ")
if `"`op'"' == `"*"' {
.setstyle , style(`prestyle') // recurse
capture numlist "`prestyle'", min(3) max(3) integer
if (_rc) .remake_as_copy
.setintensity `inten'
exit // Exit
}
.Super.setstyle, style(`scheme_setting') // let parent handle this
end
// ---------------------------------------------------------------------------
// Take over .setting and a string containing the RGB values rather than
// the current stylename.
//
// Usage: .setting
//
// Returns the current value of .rgb
program setting
class exit "`.rgb'"
end
/* -------------------------------------------------------------------------*/
/*
Usage: .setrgb #_r #_g #_b
*/
program define setrgb
numlist "`0'", integer min(3) max(3) range(>=0 <=255)
.rgb = "`r(numlist)'"
end
/* -------------------------------------------------------------------------*/
/*
Usage: .sethsb #_r #_g #_b
*/
program define sethsb
numlist "`0'", integer min(3) max(3) range(>=0 <=255)
/*
...
*/
end
/* -------------------------------------------------------------------------*/
/*
Usage: .setcmyk #_c #_m #_y #_k
*/
program define setcmyk
numlist "`0'", min(4) max(4) range(>=0 <=255)
local 0 `r(numlist)'
args c m y k
if `c' >= 0 & `c' <= 1 {
if `m' >= 0 & `m' <= 1 {
if `y' >= 0 & `y' <= 1 {
if `k' >= 0 & `k' <= 1 {
local c = int(`c' * 255)
local m = int(`m' * 255)
local y = int(`y' * 255)
local k = int(`k' * 255)
}
}
}
}
// check integer
numlist "`c' `m' `y' `k'", integer min(4) max(4) range(>=0 <=255)
local r = cond(`c'+`k' < 255, 255 - (`c'+`k'), 0)
local g = cond(`m'+`k' < 255, 255 - (`m'+`k'), 0)
local b = cond(`y'+`k' < 255, 255 - (`y'+`k'), 0)
.rgb = "`r' `g' `b'"
end
/* -------------------------------------------------------------------------*/
/*
Usage: .sethue [{+|-}]#_hue
*/
program define sethue
end
/* -------------------------------------------------------------------------*/
/*
Usage: .setsat [{+|-}]#_saturation
*/
program define setsat
end
/* -------------------------------------------------------------------------*/
/*
Usage: .setbright [{+|-}]#_brightness
*/
program define setbright
_rgb2hsv h s v
gettoken op chg : 0 , parse(" +-")
if "`op'" != "+" & "`op'" != "-" {
local v = min(1, max(0, `op'/100))
}
else {
local v = min(1, max(0, `v' `op' `chg'/100))
}
._hsv2rgb `h' `s' `v'
end
program define setvalue
.setbright `0'
end
/* -------------------------------------------------------------------------*/
/*
Usage: .setrgbhex hex_string
where hex_string is of the form FFCC80, that is to say the
same type of color setting that HTML accepts.
*/
program define setrgbhex
forvalues i = 1(2)5 {
local s = substr("`1'", `i', 2)
qui inten 16 `s'
local rgb `rgb' `r(ten)'
}
.setrgb `rgb'
end
/* -------------------------------------------------------------------------*/
/*
Usage: .setcolor color_name
Sets the color using a color name.
Here just for backward compatibility.
*/
program define setcolor
local i `.colors.codeof "`1'"'
if `i' != 0 {
.setrgbhex `.cvals[`i']'
}
end
/* -------------------------------------------------------------------------*/
/*
Usage .setintensity #
# is 0-25500
Maintains the ratio of each R,G,B and never sets any of the values
below 1 or above 255
*/
program setintensity100
args int
.setintensity `=`int'/100'
end
program setintensity0
args fint
confirm number `fint'
if `fint' == 1 {
exit
}
tokenize `.rgb'
args r g b
if `fint' > 1 {
local max = max(`r', `g', `b')
if `=round(`fint'*`max', 1)' > 255 {
local fint = 255 / `max'
}
local r = min(round(`r' * `fint', 1) , 255)
local g = min(round(`g' * `fint', 1) , 255)
local b = min(round(`b' * `fint', 1) , 255)
}
else {
local min = min(`r', `g', `b')
if `min' == 0 {
local min = min(`r', `g')
}
if `min' == 0 {
local min = min(`r', `b')
}
if `min' == 0 {
local min = min(`g', `b')
}
if `min' == 0 {
local min = max(`r', `g', `b')
}
if `=round(`fint'*`min', 1)' < 1 {
local fint = 1 / `min'
}
else {
local fint = round(`fint'*`min', 1) / `min'
}
local r = max(round(`r' * `fint', 1) , 0)
local g = max(round(`g' * `fint', 1) , 0)
local b = max(round(`b' * `fint', 1) , 0)
}
.rgb = "`r' `g' `b'"
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -