Style (color) change for order lines

Sales Order entry lines.  Currently the font color of the item number changes based on the margin of the line, I need to add a gray background when the line is closed.

I already have custom code changing the color of the text on the order lines based on Margin %, I'm trying to add color to gray the background of the field if the line is closed.  I have successfully done that however if I'm looking at a Sales Order that has line 2 closed, now has gray background, and I navigate to another Sales order that doesn't have a line 2 (it is just blank waiting for you to enter a line) that field has a gray background.  Additionally if line 1 has a gray background and I click to create a new order the new order will have line 1 (which is blank) with a gray background.  

How do I reset the field to the standard formatting if nothing is in the field?  

Last I would actually like the font to stay colored, if it should be based on the logic, and add the gray background if it is closed, the way it's working now is if it's a colored font and closed it just makes it have a gray background.

Original changes made to get the font color based on margin

$STYLE
#To add coloring for Product field
###########################################################################################################
#If GUSER = "TEMA" : Dbgaff : Endif
If func AFNC.ACTIV("YPH")
For YI=0 To [M:SOH4]NBLIG-1
If ([M:SOH4]YPFMPER(YI) <[M:SOH4]YLOWPFM(YI)) or ([M:SOH4]PFM(YI)< 0 ) Then
Chgstl [M:SOH4]ITMREF(YI) With "ISSFIYPRE"
Elsif ([M:SOH4]YLOWPFM(YI) <=[M:SOH4]YPFMPER(YI) and [M:SOH4]YPFMPER(YI) < [M:SOH4]YEXPPFM(YI)) Then
Chgstl [M:SOH4]ITMREF(YI) With "YORANGETXT"
Else
Chgstl [M:SOH4]ITMREF(YI) With "TDB00"
Endif
Next
Endif
Return

The changes I made to add the gray background when the line is closed.

$STYLE
#To add coloring for Product field
###########################################################################################################
#If GUSER = "TEMA" : Dbgaff : Endif
If func AFNC.ACTIV("YPH")
For YI=0 To [M:SOH4]NBLIG-1
if ([M:SOH4]SOQSTA(YI)=3) then ##and ([M:SOH4]ITMREF(YI) <> "") then
Chgstl [M:SOH4]ITMREF(YI) With "BACKGREY"
Elsif ([M:SOH4]YPFMPER(YI) <[M:SOH4]YLOWPFM(YI)) or ([M:SOH4]PFM(YI)< 0 ) Then
Chgstl [M:SOH4]ITMREF(YI) With "ISSFIYPRE"
Elsif ([M:SOH4]YLOWPFM(YI) <=[M:SOH4]YPFMPER(YI) and [M:SOH4]YPFMPER(YI) < [M:SOH4]YEXPPFM(YI)) Then
Chgstl [M:SOH4]ITMREF(YI) With "YORANGETXT"
Else
Chgstl [M:SOH4]ITMREF(YI) With "TDB00"
Endif
Next
Endif
Return

Thanks for your help!