By the way, it should be mentioned that in some cases I've gotten better results with g-force's version 1.0 of the script than with the 1.1.
The difference is that 1.0 tried to also balance black and white levels to the reference clip used, and in 1.1 they are locked.
Function RGBMatch(clip gref, clip bref)
{# Make RGB avg of second clip match first clip - v1.0 - by G-force
global gref = gref
global bref = bref
scriptclip(bref,"""
gref=gref.ConvertToRGB()
bref=bref.ConvertToRGB()
grefr = gref.ShowRed().ConvertToYV12()
grefg = gref.ShowGreen().ConvertToYV12()
grefb = gref.ShowBlue().ConvertToYV12()
brefr = bref.ShowRed().ConvertToYV12()
brefg = bref.ShowGreen().ConvertToYV12()
brefb = bref.ShowBlue().ConvertToYV12()
y1r = AverageLuma(grefr)
y1g = AverageLuma(grefg)
y1b = AverageLuma(grefb)
y2r = 0#Yplanemin(grefr)
y2g = 0#Yplanemin(grefg)
y2b = 0#Yplanemin(grefb)
y3r = 255#Yplanemax(grefr)
y3g = 255#Yplanemax(grefg)
y3b = 255#Yplanemax(grefb)
x1r = AverageLuma(brefr)
x1g = AverageLuma(brefg)
x1b = AverageLuma(brefb)
x2r = 0#Yplanemin(brefr)
x2g = 0#Yplanemin(brefg)
x2b = 0#Yplanemin(brefb)
x3r = 255#Yplanemax(brefr)
x3g = 255#Yplanemax(brefg)
x3b = 255#Yplanemax(brefb)
#compute coefficients for y=ax^2+bx+c using min, avg, max
ar = x1r==x2r? 0: x1r==x3r? 0: x2r==x3r? 0:
\ (y3r-y1r+(x1r-x3r)*(y1r-y2r)/(x1r-x2r))/((x1r-x3r)*(x2r-x3r))
br = x1r==x2r? 0:
\ ((y2r-y1r)/(x2r-x1r))-ar*(x1r+x2r)
cr = y1r-ar*x1r*x1r-br*x1r
ag = x1g==x2g? 0: x1g==x3g? 0: x2g==x3g? 0:
\ (y3g-y1g+(x1g-x3g)*(y1g-y2g)/(x1g-x2g))/((x1g-x3g)*(x2g-x3g))
bg = x1g==x2g? 0:
\ ((y2g-y1g)/(x2g-x1g))-ag*(x1g+x2g)
cg = y1g-ag*x1g*x1g-bg*x1g
ab = x1b==x2b? 0: x1b==x3b? 0: x2b==x3b? 0:
\ (y3b-y1b+(x1b-x3b)*(y1b-y2b)/(x1b-x2b))/((x1b-x3b)*(x2b-x3b))
bb = x1b==x2b? 0:
\ ((y2b-y1b)/(x2b-x1b))-ab*(x1b+x2b)
cb = y1b-ab*x1b*x1b-bb*x1b
brefr=brefr.MT_lut("x x * "+string(ar)+" * x "+string(br)+" * + "+string(cr)+" +").ConvertToRGB()
brefg=brefg.MT_lut("x x * "+string(ag)+" * x "+string(bg)+" * + "+string(cg)+" +").ConvertToRGB()
brefb=brefb.MT_lut("x x * "+string(ab)+" * x "+string(bb)+" * + "+string(cb)+" +").ConvertToRGB()
MergeRGB(brefr,brefg,brefb)
ConvertToYV12()
""")
Return(last)}